From a8f423e8b8bf2837033b6aedc9a50390bea87226 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sun, 23 Apr 2023 22:07:50 -0500 Subject: [PATCH] Slight Refactoring --- .../core/containers/image_view_scroll.py | 3 ++ src/new-src/core/widgets/image.py | 40 +++++++++++++++++++ src/new-src/core/widgets/image_list.py | 30 ++++---------- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/new-src/core/containers/image_view_scroll.py b/src/new-src/core/containers/image_view_scroll.py index 57417ef..da43d3f 100644 --- a/src/new-src/core/containers/image_view_scroll.py +++ b/src/new-src/core/containers/image_view_scroll.py @@ -65,6 +65,9 @@ class ImageViewScroll(Gtk.ScrolledWindow): def _do_filter_open(self, uris: [] = []): + if len(uris) == 0: + return + path, img_list = self.filter_for_images(uris) self._handle_open(path, img_list) diff --git a/src/new-src/core/widgets/image.py b/src/new-src/core/widgets/image.py index 9843edc..eda4a88 100644 --- a/src/new-src/core/widgets/image.py +++ b/src/new-src/core/widgets/image.py @@ -3,7 +3,14 @@ # Lib imports import gi gi.require_version('Gtk', '3.0') +gi.require_version('GdkPixbuf', '2.0') from gi.repository import Gtk +from gi.repository import GdkPixbuf + +try: + from PIL import Image as PImage +except Exception as e: + PImage = None # Application imports @@ -13,6 +20,8 @@ class Image(Gtk.EventBox): def __init__(self, path = None): super(Image, self).__init__() + self._thumbnail_with = settings.get_thumbnail_with() + self._thumbnail_height = settings.get_thumbnail_height() self.image = None self.path = path @@ -41,3 +50,34 @@ class Image(Gtk.EventBox): def set_image_to_view(self, widget = None, eve = None): if eve.button == 1: event_system.emit("handle_file_from_dnd", (self.path, )) + + def load_pixbuf(self): + self.set_from_pixbuf( self.get_pixbuf_data(self.path, \ + self._thumbnail_with, \ + self._thumbnail_height) ) + + def get_pixbuf_data(self, path, w = 126, h = 126): + path = self.path if not path else path + pixbuf = None + + if PImage and path.endswith(".webp"): + return self.image2pixbuf(path, w, h) + + try: + pixbuf = Gtk.Image.new_from_file(path).get_pixbuf() + except Exception: + pixbuf = Gtk.Image.new_from_resource(path).get_pixbuf() + + return pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default + + def image2pixbuf(self, path, _w, _h): + """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(_w, _h, 2) # 2 = BILINEAR and is best by default diff --git a/src/new-src/core/widgets/image_list.py b/src/new-src/core/widgets/image_list.py index b9400c3..b7a14c4 100644 --- a/src/new-src/core/widgets/image_list.py +++ b/src/new-src/core/widgets/image_list.py @@ -16,8 +16,6 @@ class ImageList(Gtk.Box): self.path = None self.img_list = None - self._thumbnail_with = settings.get_thumbnail_with() - self._thumbnail_height = settings.get_thumbnail_height() self._setup_styling() self._setup_signals() @@ -64,29 +62,17 @@ class ImageList(Gtk.Box): for file in paths: self.add( Image(file) ) - self.pre_fill_gtkimgs(paths) - - - def pre_fill_gtkimgs(self, paths: [] = []): - children = self.get_children() - for i, path in enumerate(paths): - self.load_pixbuf_to_target(path, children[i]) - + self.pre_fill_gtkimgs() self.show_range() + def pre_fill_gtkimgs(self): + children = self.get_children() + for child in children: + self.load_pixbuf_to_target(child) + @daemon_threaded - def load_pixbuf_to_target(self, path, child): - pixbuf = self.get_pixbuf_data(path, self._thumbnail_with, self._thumbnail_height) - child.set_from_pixbuf(pixbuf) - - def get_pixbuf_data(self, path, w = 126, h = 126): - pixbuf = None - try: - pixbuf = Gtk.Image.new_from_file(path).get_pixbuf() - except Exception: - pixbuf = Gtk.Image.new_from_resource(path).get_pixbuf() - - return pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default + def load_pixbuf_to_target(self, child): + child.load_pixbuf() def show_range(self, i = 0, j = 9): children = self.get_children()