130 lines
3.9 KiB
Python
130 lines
3.9 KiB
Python
# Python imports
|
|
from os.path import getsize
|
|
import inspect
|
|
|
|
# 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
|
|
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.pixbuff = None
|
|
self.work_pixbuff = None
|
|
self.fit_to_win = True
|
|
self.animation = None
|
|
self.playing_animation = False
|
|
|
|
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("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_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)
|
|
|
|
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:
|
|
self._play_animation()
|
|
|
|
def load_path(self, path):
|
|
self.pixbuff = None
|
|
self.work_pixbuff = None
|
|
self.animation = None
|
|
|
|
self._stop_animation()
|
|
if path.endswith(".gif"):
|
|
self.set_as_gif(path)
|
|
return
|
|
|
|
if PImage and path.endswith(".webp"):
|
|
self.set_as_webp(path)
|
|
|
|
if not self.work_pixbuff:
|
|
self.set_as_static(path)
|
|
|
|
self.pixbuff = self.work_pixbuff.copy()
|
|
self.pixbuff.path = path
|
|
|
|
width = self.pixbuff.get_width()
|
|
height = self.pixbuff.get_height()
|
|
size = sizeof_fmt( getsize(path) )
|
|
path = f"{path} | {width} x {height} | {size}"
|
|
event_system.emit("update_path_label", (path,))
|
|
|
|
if self.fit_to_win:
|
|
self._fit_to_container()
|
|
else:
|
|
self._scale_1_two_1()
|
|
|
|
def set_as_gif(self, path):
|
|
image = None
|
|
try:
|
|
image = GdkPixbuf.PixbufAnimation.new_from_file(path)
|
|
except Exception:
|
|
image = GdkPixbuf.PixbufAnimation.new_from_resource(path)
|
|
|
|
self.animation = image.get_iter()
|
|
|
|
def set_as_static(self, path):
|
|
try:
|
|
self.work_pixbuff = Gtk.Image.new_from_file(path).get_pixbuf()
|
|
except Exception:
|
|
self.work_pixbuff = Gtk.Image.new_from_resource(path).get_pixbuf()
|
|
|
|
def set_as_webp(self, path):
|
|
self.work_pixbuff = self.image2pixbuf(path)
|
|
|
|
@staticmethod
|
|
def image2pixbuf(path):
|
|
"""Convert Pillow image to GdkPixbuf"""
|
|
im = PImage.open(path)
|
|
data = im.tobytes()
|
|
data = Bytes.new(data)
|
|
w, h = im.size
|
|
|
|
return GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB,
|
|
False, 8, w, h, w * 3) |