2023-04-23 16:47:33 -05:00
|
|
|
# Python imports
|
2023-09-24 16:39:18 -05:00
|
|
|
from os.path import getsize
|
2023-04-27 22:45:20 -05:00
|
|
|
import inspect
|
2023-04-23 16:47:33 -05:00
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
import gi
|
|
|
|
|
gi.require_version('Gtk', '3.0')
|
2023-04-27 17:57:23 -05:00
|
|
|
gi.require_version('GdkPixbuf', '2.0')
|
2023-04-23 16:47:33 -05:00
|
|
|
from gi.repository import Gtk
|
2023-04-24 22:31:05 -05:00
|
|
|
from gi.repository import GdkPixbuf
|
2023-04-27 20:01:53 -05:00
|
|
|
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
|
2023-04-23 16:47:33 -05:00
|
|
|
|
|
|
|
|
# Application imports
|
2023-04-27 20:01:53 -05:00
|
|
|
from .image_view_mixin import ImageViewMixin
|
2023-04-23 16:47:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-27 20:01:53 -05:00
|
|
|
class ImageView(ImageViewMixin, Gtk.Image):
|
2023-04-23 16:47:33 -05:00
|
|
|
def __init__(self):
|
|
|
|
|
super(ImageView, self).__init__()
|
|
|
|
|
|
2023-04-27 22:45:20 -05:00
|
|
|
self.pixbuff = None
|
|
|
|
|
self.work_pixbuff = None
|
|
|
|
|
self.fit_to_win = True
|
|
|
|
|
self.animation = None
|
|
|
|
|
self.playing_animation = False
|
2023-04-23 16:47:33 -05:00
|
|
|
|
|
|
|
|
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):
|
2026-05-22 18:27:16 -05:00
|
|
|
event_system.subscribe("size-allocate", self._size_allocate)
|
|
|
|
|
event_system.subscribe("handle-file-from-dnd", self._handle_file_from_dnd)
|
|
|
|
|
|
|
|
|
|
event_system.subscribe("get-active-image-path", self._get_active_image_path)
|
|
|
|
|
event_system.subscribe("zoom-out", self._zoom_out)
|
|
|
|
|
event_system.subscribe("rotate-left", self._rotate_left)
|
|
|
|
|
event_system.subscribe("vertical-flip", self._vertical_flip)
|
|
|
|
|
event_system.subscribe("scale-1-to-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)
|
2023-04-23 16:47:33 -05:00
|
|
|
|
|
|
|
|
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:
|
2023-04-27 22:45:20 -05:00
|
|
|
self._play_animation()
|
2023-04-23 16:47:33 -05:00
|
|
|
|
|
|
|
|
def load_path(self, path):
|
2023-04-27 22:45:20 -05:00
|
|
|
self.pixbuff = None
|
|
|
|
|
self.work_pixbuff = None
|
|
|
|
|
self.animation = None
|
2023-04-23 16:47:33 -05:00
|
|
|
|
2023-04-27 22:45:20 -05:00
|
|
|
self._stop_animation()
|
2023-04-23 16:47:33 -05:00
|
|
|
if path.endswith(".gif"):
|
2023-04-27 20:01:53 -05:00
|
|
|
self.set_as_gif(path)
|
2023-04-27 22:45:20 -05:00
|
|
|
return
|
2023-04-23 16:47:33 -05:00
|
|
|
|
2023-04-27 20:01:53 -05:00
|
|
|
if PImage and path.endswith(".webp"):
|
|
|
|
|
self.set_as_webp(path)
|
2023-04-23 16:47:33 -05:00
|
|
|
|
2023-04-27 22:45:20 -05:00
|
|
|
if not self.work_pixbuff:
|
2023-04-27 20:01:53 -05:00
|
|
|
self.set_as_static(path)
|
2023-04-25 16:12:30 -05:00
|
|
|
|
2024-03-15 23:34:58 -05:00
|
|
|
self.pixbuff = self.work_pixbuff.copy()
|
|
|
|
|
self.pixbuff.path = path
|
|
|
|
|
|
2023-09-24 16:39:18 -05:00
|
|
|
width = self.pixbuff.get_width()
|
|
|
|
|
height = self.pixbuff.get_height()
|
2026-05-22 18:27:16 -05:00
|
|
|
size = self.sizeof_fmt( getsize(path) )
|
2023-09-24 16:39:18 -05:00
|
|
|
path = f"{path} | {width} x {height} | {size}"
|
2026-05-22 18:27:16 -05:00
|
|
|
event_system.emit("update-path-label", (path,))
|
2023-09-24 16:39:18 -05:00
|
|
|
|
2023-04-25 16:12:30 -05:00
|
|
|
if self.fit_to_win:
|
|
|
|
|
self._fit_to_container()
|
2023-04-28 21:09:44 -05:00
|
|
|
else:
|
|
|
|
|
self._scale_1_two_1()
|
2023-04-23 16:47:33 -05:00
|
|
|
|
2026-05-22 18:27:16 -05:00
|
|
|
def sizeof_fmt(self, num, suffix = "B"):
|
|
|
|
|
for unit in ["", "K", "M", "G", "T", "Pi", "Ei", "Zi"]:
|
|
|
|
|
if abs(num) < 1024.0:
|
|
|
|
|
return f"{num:3.1f} {unit}{suffix}"
|
|
|
|
|
num /= 1024.0
|
|
|
|
|
return f"{num:.1f} Yi{suffix}"
|
|
|
|
|
|
2023-04-27 20:01:53 -05:00
|
|
|
def set_as_gif(self, path):
|
2023-04-27 22:45:20 -05:00
|
|
|
image = None
|
2023-04-27 20:01:53 -05:00
|
|
|
try:
|
2023-04-27 22:45:20 -05:00
|
|
|
image = GdkPixbuf.PixbufAnimation.new_from_file(path)
|
2023-04-27 20:01:53 -05:00
|
|
|
except Exception:
|
2023-04-27 22:45:20 -05:00
|
|
|
image = GdkPixbuf.PixbufAnimation.new_from_resource(path)
|
|
|
|
|
|
|
|
|
|
self.animation = image.get_iter()
|
2023-04-27 20:01:53 -05:00
|
|
|
|
|
|
|
|
def set_as_static(self, path):
|
|
|
|
|
try:
|
2023-04-27 22:45:20 -05:00
|
|
|
self.work_pixbuff = Gtk.Image.new_from_file(path).get_pixbuf()
|
2023-04-27 20:01:53 -05:00
|
|
|
except Exception:
|
2023-04-27 22:45:20 -05:00
|
|
|
self.work_pixbuff = Gtk.Image.new_from_resource(path).get_pixbuf()
|
|
|
|
|
|
2023-04-27 20:01:53 -05:00
|
|
|
def set_as_webp(self, path):
|
2023-04-28 19:40:16 -05:00
|
|
|
self.work_pixbuff = self.image2pixbuf(path)
|
2023-04-27 20:01:53 -05:00
|
|
|
|
|
|
|
|
@staticmethod
|
2023-04-28 19:40:16 -05:00
|
|
|
def image2pixbuf(path):
|
2023-04-27 20:01:53 -05:00
|
|
|
"""Convert Pillow image to GdkPixbuf"""
|
|
|
|
|
im = PImage.open(path)
|
|
|
|
|
data = im.tobytes()
|
|
|
|
|
data = Bytes.new(data)
|
|
|
|
|
w, h = im.size
|
|
|
|
|
|
2023-04-28 19:40:16 -05:00
|
|
|
return GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB,
|
2026-05-22 18:27:16 -05:00
|
|
|
False, 8, w, h, w * 3)
|