2023-04-24 02:18:35 +00:00
|
|
|
# Python imports
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
2023-04-24 03:07:50 +00:00
|
|
|
gi.require_version('GdkPixbuf', '2.0')
|
2023-04-24 02:18:35 +00:00
|
|
|
from gi.repository import Gtk
|
2023-04-24 04:35:23 +00:00
|
|
|
from gi.repository import GLib
|
2023-04-24 03:07:50 +00:00
|
|
|
from gi.repository import GdkPixbuf
|
|
|
|
|
|
|
|
try:
|
|
|
|
from PIL import Image as PImage
|
|
|
|
except Exception as e:
|
|
|
|
PImage = None
|
2023-04-24 02:18:35 +00:00
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Image(Gtk.EventBox):
|
|
|
|
def __init__(self, path = None):
|
|
|
|
super(Image, self).__init__()
|
|
|
|
|
2023-04-24 03:07:50 +00:00
|
|
|
self._thumbnail_with = settings.get_thumbnail_with()
|
|
|
|
self._thumbnail_height = settings.get_thumbnail_height()
|
2023-04-24 05:12:56 +00:00
|
|
|
self.is_loaded = False
|
|
|
|
self.image = None
|
|
|
|
self.path = path
|
2023-04-24 02:18:35 +00:00
|
|
|
|
|
|
|
self._setup_styling()
|
|
|
|
self._setup_signals()
|
|
|
|
self._load_widgets()
|
|
|
|
|
|
|
|
|
|
|
|
def _setup_styling(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
def _setup_signals(self):
|
|
|
|
self.connect("button-release-event", self.set_image_to_view)
|
|
|
|
|
|
|
|
def _subscribe_to_events(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
def _load_widgets(self):
|
|
|
|
self.image = Gtk.Image()
|
|
|
|
self.image.show()
|
|
|
|
self.add(self.image)
|
|
|
|
|
|
|
|
def set_image_to_view(self, widget = None, eve = None):
|
|
|
|
if eve.button == 1:
|
|
|
|
event_system.emit("handle_file_from_dnd", (self.path, ))
|
2023-04-24 03:07:50 +00:00
|
|
|
|
|
|
|
def load_pixbuf(self):
|
|
|
|
self.set_from_pixbuf( self.get_pixbuf_data(self.path, \
|
|
|
|
self._thumbnail_with, \
|
|
|
|
self._thumbnail_height) )
|
2023-04-24 05:12:56 +00:00
|
|
|
self.is_loaded = True
|
2023-04-24 03:07:50 +00:00
|
|
|
|
2023-04-25 03:31:05 +00:00
|
|
|
def set_from_pixbuf(self, pixbuf):
|
|
|
|
self.image.set_from_pixbuf(pixbuf)
|
|
|
|
|
2023-04-24 03:07:50 +00:00
|
|
|
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
|