# Python imports # Lib imports import gi gi.require_version('Gtk', '3.0') gi.require_version('Gdk', '3.0') gi.require_version('GdkPixbuf', '2.0') from gi.repository import Gtk from gi.repository import Gdk from gi.repository import GdkPixbuf from gi.repository.GLib import Bytes try: from PIL import Image as PImage logger.debug("Pillow library exists. Loading PIL as PImage...") except Exception as e: logger.debug("No Pillow library found. Loading PImage as None...") PImage = None # Application imports from .image_view_mixin import ImageViewMixin class ImageView(ImageViewMixin, Gtk.Image): def __init__(self): super(ImageView, self).__init__() self.pixbuf = None self.work_pixbuf = None self.animation = None self.fit_to_win = True self._setup_styling() self._setup_signals() self._subscribe_to_events() self._load_widgets() self.show_all() def _setup_styling(self): ... def _setup_signals(self): ... def _subscribe_to_events(self): event_system.subscribe("zoom_out", self._zoom_out) event_system.subscribe("handle_file_from_dnd", self._handle_file_from_dnd) event_system.subscribe("vertical_flip", self._vertical_flip) event_system.subscribe("rotate_left", self._rotate_left) event_system.subscribe("scale_1_two_1", self._scale_1_two_1) event_system.subscribe("fit_to_container", self._fit_to_container) event_system.subscribe("horizontal_flip", self._horizontal_flip) event_system.subscribe("rotate_right", self._rotate_right) event_system.subscribe("zoom_in", self._zoom_in) event_system.subscribe("size_allocate", self._size_allocate) def _load_widgets(self): ... def _handle_file_from_dnd(self, path = None): logger.debug(f"Loading image from: {path}") self.load_path(path) if self.animation: logger.debug("Start animation stub...") def load_path(self, path): self.pixbuf = None self.work_pixbuf = None self.animation = None event_system.emit("update_path_label", (path,)) if path.endswith(".gif"): self.set_as_gif(path) if PImage and path.endswith(".webp"): self.set_as_webp(path) if not self.work_pixbuf: self.set_as_static(path) self.pixbuf = self.work_pixbuf if self.animation: ... else: self.set_from_pixbuf(self.work_pixbuf) if self.fit_to_win: self._fit_to_container() def set_as_gif(self, path): self.work_pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path).get_static_image() try: self.animation = Gtk.Image.new_from_file(path).get_animation() except Exception: self.animation = Gtk.Image.new_from_resource(path).get_animation() def set_as_static(self, path): try: self.work_pixbuf = Gtk.Image.new_from_file(path).get_pixbuf() except Exception: self.work_pixbuf = Gtk.Image.new_from_resource(path).get_pixbuf() def set_as_webp(self, path): w = settings.get_thumbnail_with() h = settings.get_thumbnail_height() print("shit") self.work_pixbuf = self.image2pixbuf(path, w, h) @staticmethod def image2pixbuf(path, _w, _h): """Convert Pillow image to GdkPixbuf""" im = PImage.open(path) data = im.tobytes() data = 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