From 9fdb029b06db302ba0bb0ab553940c0954e9dbf6 Mon Sep 17 00:00:00 2001 From: Klemek Date: Fri, 27 Aug 2021 11:20:01 +0200 Subject: [PATCH] bulk bmp import/export --- watchy-image-editor/app.py | 28 ++++++++++++++++++++++++++-- watchy-image-editor/file.py | 8 +++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/watchy-image-editor/app.py b/watchy-image-editor/app.py index a1b5a2c..1e130c1 100644 --- a/watchy-image-editor/app.py +++ b/watchy-image-editor/app.py @@ -69,12 +69,12 @@ class App(ttk.Frame): "Bulk .bmp Import...", "_bmp_import_all", MenuEntryType.NEED_FILE, - ), # TODO _bmp_import_all + ), ( "Export All To .bmp...", "_bmp_export_all", MenuEntryType.NEED_FILE, - ), # TODO _bmp_export_all + ), ("", "", MenuEntryType.SEPARATOR), ( "Import .bmp Into Image...", @@ -268,6 +268,30 @@ class App(ttk.Frame): def _image_delete(self) -> None: self.explorer.delete() + def _bmp_import_all(self) -> None: + paths = filedialog.askopenfilenames( + filetypes=Bitmap.FILE_TYPES, + defaultextension=Bitmap.FILE_TYPES, + ) + if paths and len(paths) > 0: + for path in paths: + name = os.path.basename(path).rstrip(".bmp") + image = self.current_file.search(name) + if image is None: + image = Image(name, 20, 20, empty=True) + self.current_file.images += [image] + try: + image.import_bmp(path) + except BitmapError as e: + pass + self.update() + + def _bmp_export_all(self) -> None: + dir_path = filedialog.askdirectory() + if dir_path: + for image in self.current_file.images: + image.export_bmp(os.path.join(dir_path, f"{image.name}.bmp")) + def _bmp_import_image(self) -> None: path = filedialog.askopenfilename( filetypes=Bitmap.FILE_TYPES, diff --git a/watchy-image-editor/file.py b/watchy-image-editor/file.py index 5937186..f3a130f 100644 --- a/watchy-image-editor/file.py +++ b/watchy-image-editor/file.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Optional import re import os.path @@ -25,6 +25,12 @@ class File: def modified(self) -> bool: return any(image.modified for image in self.images) + def search(self, name) -> Optional[Image]: + for image in self.images: + if image.name == name: + return image + return None + def __read_file(self) -> List[Image]: images = []