WebP support, smal refactor of image_view

This commit is contained in:
2023-04-27 20:01:53 -05:00
parent 599d1f5145
commit 3fc6fbae86
3 changed files with 139 additions and 107 deletions

View File

@@ -8,12 +8,9 @@ from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import GdkPixbuf
try:
from PIL import Image as PImage
except Exception as e:
PImage = None
# Application imports
from .image_view import ImageView, PImage
@@ -64,7 +61,7 @@ class Image(Gtk.EventBox):
pixbuf = None
if PImage and path.endswith(".webp"):
return self.image2pixbuf(path, w, h)
return ImageView.image2pixbuf(path, w, h)
if path.endswith(".gif"):
pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path).get_static_image()
@@ -76,15 +73,3 @@ class Image(Gtk.EventBox):
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