From 5a18748897685b45dad1a74ffccd0335a20da673 Mon Sep 17 00:00:00 2001 From: klemek Date: Wed, 25 Aug 2021 15:32:31 +0200 Subject: [PATCH] fix export and redraw --- watchy-image-editor/main.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/watchy-image-editor/main.py b/watchy-image-editor/main.py index d3c64ac..978b6c1 100644 --- a/watchy-image-editor/main.py +++ b/watchy-image-editor/main.py @@ -58,13 +58,12 @@ class Bitmap: elif len(data) > size: data = data[:size] line_padding = (width * 3) % 4 - if line_padding == 0: - return data output_data = bytes() for y in range(height): - start = y * 3 * width - output_data += data[start : start + width] - output_data += bytes([0]) * line_padding + start = (height - y - 1) * 3 * width + output_data += data[start : start + width * 3] + if line_padding > 0: + output_data += bytes([0]) * (4 - line_padding) return output_data @@ -117,6 +116,10 @@ class Image: output += bytes([255, 255, 255]) return output + def export_bmp(self, path: str) -> None: + with open(path, mode="wb") as f: + f.write(Bitmap.get_bmp_data(self.width, self.get_color_bytes())) + class File: FILE_TYPES = [("Header File", "*.h"), ("All Files", "*.*")] @@ -334,14 +337,7 @@ class App(ttk.Frame): height=(image.height * DRAW_SCALE), background="white", ) - self.canvas.create_rectangle( - 0, - 0, - (image.width * DRAW_SCALE), - (image.height * DRAW_SCALE), - fill="white", - outline="", - ) + self.canvas.delete("all") for x in range(image.width): for y in range(image.height): if image.get_pixel(x, y): @@ -431,13 +427,7 @@ class App(ttk.Frame): initialfile=f"{self.current_image.name}.bmp", ) if path is not None: - with open(path, mode="wb") as f: - f.write( - Bitmap.get_bmp_data( - self.current_image.width, self.current_image.get_color_bytes() - ) - ) - + self.current_image.export_bmp(path) if __name__ == "__main__": app = App(Tk())