From fd7d1f1e95bd4380600f966c950b3cb97626fb66 Mon Sep 17 00:00:00 2001 From: mashed5894 Date: Fri, 1 Aug 2025 09:53:35 +0300 Subject: [PATCH] feat(ui): krita/open raster thumbs (#985) * added kra/krz media categories and thumb rendering logic * added .ora support (follows open doc standard) --------- Co-authored-by: fj92hg <> --- src/tagstudio/core/media_types.py | 9 +++++++ src/tagstudio/qt/widgets/thumb_renderer.py | 28 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/tagstudio/core/media_types.py b/src/tagstudio/core/media_types.py index ade4e858..3a8d0081 100644 --- a/src/tagstudio/core/media_types.py +++ b/src/tagstudio/core/media_types.py @@ -108,6 +108,7 @@ class MediaCategories: ".psd", } _AFFINITY_PHOTO_SET: set[str] = {".afphoto"} + _KRITA_SET: set[str] = {".kra", ".krz"} _ARCHIVE_SET: set[str] = { ".7z", ".gz", @@ -341,6 +342,7 @@ class MediaCategories: ".odp", ".ods", ".odt", + ".ora", } _PACKAGE_SET: set[str] = { ".aab", @@ -605,6 +607,12 @@ class MediaCategories: is_iana=True, name="video", ) + KRITA_TYPES = MediaCategory( + media_type=MediaType.IMAGE, + extensions=_KRITA_SET, + is_iana=False, + name="krita", + ) ALL_CATEGORIES = [ ADOBE_PHOTOSHOP_TYPES, @@ -639,6 +647,7 @@ class MediaCategories: SPREADSHEET_TYPES, TEXT_TYPES, VIDEO_TYPES, + KRITA_TYPES, ] @staticmethod diff --git a/src/tagstudio/qt/widgets/thumb_renderer.py b/src/tagstudio/qt/widgets/thumb_renderer.py index e4ec1e8d..f0be2a09 100644 --- a/src/tagstudio/qt/widgets/thumb_renderer.py +++ b/src/tagstudio/qt/widgets/thumb_renderer.py @@ -677,6 +677,29 @@ class ThumbRenderer(QObject): return im + @staticmethod + def _krita_thumb(filepath: Path) -> Image.Image: + """Extract and render a thumbnail for an Krita file. + + Args: + filepath (Path): The path of the file. + """ + file_path_within_zip = "preview.png" + im: Image.Image = None + with zipfile.ZipFile(filepath, "r") as zip_file: + # Check if the file exists in the zip + if file_path_within_zip in zip_file.namelist(): + # Read the specific file into memory + file_data = zip_file.read(file_path_within_zip) + thumb_im = Image.open(BytesIO(file_data)) + if thumb_im: + im = Image.new("RGB", thumb_im.size, color="#1e1e1e") + im.paste(thumb_im) + else: + logger.error("Couldn't render thumbnail", filepath=filepath) + + return im + @staticmethod def _powerpoint_thumb(filepath: Path) -> Image.Image | None: """Extract and render a thumbnail for a Microsoft PowerPoint file. @@ -1366,6 +1389,11 @@ class ThumbRenderer(QObject): # PowerPoint Slideshow elif ext in {".pptx"}: image = self._powerpoint_thumb(_filepath) + # Krita ======================================================== + elif MediaCategories.is_ext_in_category( + ext, MediaCategories.KRITA_TYPES, mime_fallback=True + ): + image = self._krita_thumb(_filepath) # OpenDocument/OpenOffice ====================================== elif MediaCategories.is_ext_in_category( ext, MediaCategories.OPEN_DOCUMENT_TYPES, mime_fallback=True