From 4c4cb051d8d71c1bb8d9e1a63e4237656320d907 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Tue, 21 Feb 2023 16:26:04 -0600 Subject: [PATCH] added webp support, cleaned up configs --- src/context/controller.py | 25 ++++++++++++++++++++++++- src/context/icons/icon.py | 25 +++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/context/controller.py b/src/context/controller.py index b43b1cf..c45a227 100644 --- a/src/context/controller.py +++ b/src/context/controller.py @@ -13,6 +13,12 @@ from .controller_data import Controller_Data from .mixins.tree_view_update_mixin import TreeViewUpdateMixin +try: + from PIL import Image as PImage +except Exception as e: + PImage = None + + def threaded(fn): def wrapper(*args, **kwargs): threading.Thread(target=fn, args=args, kwargs=kwargs).start() @@ -119,8 +125,25 @@ class Controller(TreeViewUpdateMixin, Controller_Data): geom_rec = self.image_area.get_parent().get_parent().get_allocated_size()[0] width = geom_rec.width - 15 height = geom_rec.height - 15 + wxh = [width, height] + self.image_area.set_size_request(width, height) - return GdkPixbuf.Pixbuf.new_from_file_at_scale(uri, width, height, True) + if PImage and uri.lower().endswith(".webp"): + return self.image2pixbuf(uri, wxh) + else: + return GdkPixbuf.Pixbuf.new_from_file_at_scale(uri, width, height, True) + + def image2pixbuf(self, path, wxh): + """Convert Pillow image to GdkPixbuf""" + im = PImage.open(path) + data = im.tobytes() + data = GLib.Bytes.new(data) + w, h = im.size + + pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB, + False, 8, w, h, w * 3) + + return pixbuf.scale_simple(wxh[0], wxh[1], 2) # BILINEAR = 2 def _load_image(self): self.clear_children(self.image_area) diff --git a/src/context/icons/icon.py b/src/context/icons/icon.py index 2f3ba4c..300fd8a 100644 --- a/src/context/icons/icon.py +++ b/src/context/icons/icon.py @@ -5,13 +5,19 @@ from os.path import isfile # Gtk imports import gi gi.require_version('GdkPixbuf', '2.0') -from gi.repository import GdkPixbuf +from gi.repository import GdkPixbuf, GLib # Application imports from .mixins.desktopiconmixin import DesktopIconMixin from .mixins.videoiconmixin import VideoIconMixin +try: + from PIL import Image as PImage +except Exception as e: + PImage = None + + def threaded(fn): def wrapper(*args, **kwargs): threading.Thread(target=fn, args=args, kwargs=kwargs).start() @@ -60,12 +66,27 @@ class Icon(DesktopIconMixin, VideoIconMixin): .get_static_image() \ .scale_simple(wxh[0], wxh[1], GdkPixbuf.InterpType.BILINEAR) else: - return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], True) + if PImage and path.lower().endswith(".webp"): + return self.image2pixbuf(path, wxh) + else: + return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], True) except Exception as e: print("Image Scaling Issue:") print( repr(e) ) return None + def image2pixbuf(self, path, wxh): + """Convert Pillow image to GdkPixbuf""" + im = PImage.open(path) + data = im.tobytes() + data = GLib.Bytes.new(data) + w, h = im.size + + pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB, + False, 8, w, h, w * 3) + + return pixbuf.scale_simple(wxh[0], wxh[1], 2) # BILINEAR = 2 + def create_from_file(self, path): try: return GdkPixbuf.Pixbuf.new_from_file(path)