WebP support, smal refactor of image_view
This commit is contained in:
parent
599d1f5145
commit
3fc6fbae86
@ -8,12 +8,9 @@ from gi.repository import Gtk
|
|||||||
from gi.repository import GLib
|
from gi.repository import GLib
|
||||||
from gi.repository import GdkPixbuf
|
from gi.repository import GdkPixbuf
|
||||||
|
|
||||||
try:
|
|
||||||
from PIL import Image as PImage
|
|
||||||
except Exception as e:
|
|
||||||
PImage = None
|
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
|
from .image_view import ImageView, PImage
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -64,7 +61,7 @@ class Image(Gtk.EventBox):
|
|||||||
pixbuf = None
|
pixbuf = None
|
||||||
|
|
||||||
if PImage and path.endswith(".webp"):
|
if PImage and path.endswith(".webp"):
|
||||||
return self.image2pixbuf(path, w, h)
|
return ImageView.image2pixbuf(path, w, h)
|
||||||
|
|
||||||
if path.endswith(".gif"):
|
if path.endswith(".gif"):
|
||||||
pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path).get_static_image()
|
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()
|
pixbuf = Gtk.Image.new_from_resource(path).get_pixbuf()
|
||||||
|
|
||||||
return pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
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
|
|
||||||
|
@ -8,12 +8,22 @@ gi.require_version('GdkPixbuf', '2.0')
|
|||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
from gi.repository import Gdk
|
from gi.repository import Gdk
|
||||||
from gi.repository import GdkPixbuf
|
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
|
# Application imports
|
||||||
|
from .image_view_mixin import ImageViewMixin
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ImageView(Gtk.Image):
|
class ImageView(ImageViewMixin, Gtk.Image):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(ImageView, self).__init__()
|
super(ImageView, self).__init__()
|
||||||
|
|
||||||
@ -65,108 +75,54 @@ class ImageView(Gtk.Image):
|
|||||||
self.work_pixbuf = None
|
self.work_pixbuf = None
|
||||||
self.animation = None
|
self.animation = None
|
||||||
|
|
||||||
|
event_system.emit("update_path_label", (path,))
|
||||||
if path.endswith(".gif"):
|
if path.endswith(".gif"):
|
||||||
self.work_pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path).get_static_image()
|
self.set_as_gif(path)
|
||||||
try:
|
|
||||||
self.animation = Gtk.Image.new_from_file(path).get_animation()
|
|
||||||
except Exception:
|
|
||||||
self.animation = Gtk.Image.new_from_resource(path).get_animation()
|
|
||||||
|
|
||||||
|
if PImage and path.endswith(".webp"):
|
||||||
|
self.set_as_webp(path)
|
||||||
|
|
||||||
if not self.work_pixbuf:
|
if not self.work_pixbuf:
|
||||||
try:
|
self.set_as_static(path)
|
||||||
self.work_pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
|
|
||||||
except Exception:
|
|
||||||
self.work_pixbuf = Gtk.Image.new_from_resource(path).get_pixbuf()
|
|
||||||
|
|
||||||
self.pixbuf = self.work_pixbuf
|
self.pixbuf = self.work_pixbuf
|
||||||
self.set_from_pixbuf(self.work_pixbuf)
|
if self.animation:
|
||||||
|
...
|
||||||
|
else:
|
||||||
|
self.set_from_pixbuf(self.work_pixbuf)
|
||||||
|
|
||||||
if self.fit_to_win:
|
if self.fit_to_win:
|
||||||
self._fit_to_container()
|
self._fit_to_container()
|
||||||
|
|
||||||
event_system.emit("update_path_label", (path,))
|
|
||||||
|
|
||||||
|
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 _zoom_out(self):
|
def set_as_static(self, path):
|
||||||
if self.work_pixbuf and self.pixbuf:
|
try:
|
||||||
# TODO: Setup scale factor setting to pull from settings...
|
self.work_pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
|
||||||
stepx = self.work_pixbuf.get_width() * 0.05
|
except Exception:
|
||||||
stepy = self.work_pixbuf.get_height() * 0.05
|
self.work_pixbuf = Gtk.Image.new_from_resource(path).get_pixbuf()
|
||||||
|
|
||||||
w = self.work_pixbuf.get_width() - stepx
|
def set_as_webp(self, path):
|
||||||
h = self.work_pixbuf.get_height() - stepy
|
w = settings.get_thumbnail_with()
|
||||||
|
h = settings.get_thumbnail_height()
|
||||||
|
print("shit")
|
||||||
|
self.work_pixbuf = self.image2pixbuf(path, w, h)
|
||||||
|
|
||||||
self.work_pixbuf = self.pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
@staticmethod
|
||||||
self.set_from_pixbuf(self.work_pixbuf)
|
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
|
||||||
|
|
||||||
def _rotate_left(self):
|
pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB,
|
||||||
if self.work_pixbuf and self.pixbuf:
|
False, 8, w, h, w * 3)
|
||||||
self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
|
|
||||||
self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
|
|
||||||
self.set_from_pixbuf(self.work_pixbuf)
|
|
||||||
|
|
||||||
def _vertical_flip(self):
|
return pixbuf.scale_simple(_w, _h, 2) # 2 = BILINEAR and is best by default
|
||||||
if self.work_pixbuf and self.pixbuf:
|
|
||||||
self.work_pixbuf = self.work_pixbuf.flip(True)
|
|
||||||
self.pixbuf = self.pixbuf.flip(True)
|
|
||||||
self.set_from_pixbuf(self.work_pixbuf)
|
|
||||||
|
|
||||||
def _scale_1_two_1(self):
|
|
||||||
self.fit_to_win = False
|
|
||||||
if self.work_pixbuf and self.pixbuf:
|
|
||||||
self.work_pixbuf = self.pixbuf
|
|
||||||
self.set_from_pixbuf(self.work_pixbuf)
|
|
||||||
|
|
||||||
def _fit_to_container(self):
|
|
||||||
self.fit_to_win = True
|
|
||||||
if self.work_pixbuf and self.pixbuf:
|
|
||||||
parent_aloc = self.get_parent().get_parent().get_allocation()
|
|
||||||
pw = parent_aloc.width
|
|
||||||
ph = parent_aloc.height
|
|
||||||
iw = self.pixbuf.get_width()
|
|
||||||
ih = self.pixbuf.get_height()
|
|
||||||
w = 0
|
|
||||||
h = 0
|
|
||||||
|
|
||||||
if iw == 0 or ih == 0:
|
|
||||||
return
|
|
||||||
|
|
||||||
w = pw;
|
|
||||||
h = ((ih * w) / iw + 0.5)
|
|
||||||
|
|
||||||
if h > ph:
|
|
||||||
h = ph
|
|
||||||
w = (iw * h) / ih + 0.5
|
|
||||||
|
|
||||||
|
|
||||||
self.work_pixbuf = self.pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
|
||||||
self.set_from_pixbuf(self.work_pixbuf)
|
|
||||||
|
|
||||||
def _horizontal_flip(self):
|
|
||||||
if self.work_pixbuf and self.pixbuf:
|
|
||||||
self.work_pixbuf = self.work_pixbuf.flip(False)
|
|
||||||
self.pixbuf = self.pixbuf.flip(False)
|
|
||||||
self.set_from_pixbuf(self.work_pixbuf)
|
|
||||||
|
|
||||||
def _rotate_right(self):
|
|
||||||
if self.work_pixbuf and self.pixbuf:
|
|
||||||
self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
|
|
||||||
self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
|
|
||||||
self.set_from_pixbuf(self.work_pixbuf)
|
|
||||||
|
|
||||||
def _zoom_in(self):
|
|
||||||
if self.work_pixbuf and self.pixbuf:
|
|
||||||
# TODO: Setup scale factor setting to pull from settings...
|
|
||||||
stepx = self.work_pixbuf.get_width() * 0.05
|
|
||||||
stepy = self.work_pixbuf.get_height() * 0.05
|
|
||||||
|
|
||||||
w = self.work_pixbuf.get_width() + stepx
|
|
||||||
h = self.work_pixbuf.get_height() + stepy
|
|
||||||
|
|
||||||
self.work_pixbuf = self.pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
|
||||||
self.set_from_pixbuf(self.work_pixbuf)
|
|
||||||
|
|
||||||
def _size_allocate(self):
|
|
||||||
if self.fit_to_win:
|
|
||||||
self._fit_to_container()
|
|
||||||
|
91
src/core/widgets/image_view_mixin.py
Normal file
91
src/core/widgets/image_view_mixin.py
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
# Python imports
|
||||||
|
|
||||||
|
# Lib imports
|
||||||
|
|
||||||
|
# Application imports
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class ImageViewMixin:
|
||||||
|
def _zoom_out(self):
|
||||||
|
if self.work_pixbuf and self.pixbuf:
|
||||||
|
# TODO: Setup scale factor setting to pull from settings...
|
||||||
|
stepx = self.work_pixbuf.get_width() * 0.05
|
||||||
|
stepy = self.work_pixbuf.get_height() * 0.05
|
||||||
|
|
||||||
|
w = self.work_pixbuf.get_width() - stepx
|
||||||
|
h = self.work_pixbuf.get_height() - stepy
|
||||||
|
|
||||||
|
self.work_pixbuf = self.pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
||||||
|
self.set_from_pixbuf(self.work_pixbuf)
|
||||||
|
|
||||||
|
def _rotate_left(self):
|
||||||
|
if self.work_pixbuf and self.pixbuf:
|
||||||
|
self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
|
||||||
|
self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
|
||||||
|
self.set_from_pixbuf(self.work_pixbuf)
|
||||||
|
|
||||||
|
def _vertical_flip(self):
|
||||||
|
if self.work_pixbuf and self.pixbuf:
|
||||||
|
self.work_pixbuf = self.work_pixbuf.flip(True)
|
||||||
|
self.pixbuf = self.pixbuf.flip(True)
|
||||||
|
self.set_from_pixbuf(self.work_pixbuf)
|
||||||
|
|
||||||
|
def _scale_1_two_1(self):
|
||||||
|
self.fit_to_win = False
|
||||||
|
if self.work_pixbuf and self.pixbuf:
|
||||||
|
self.work_pixbuf = self.pixbuf
|
||||||
|
self.set_from_pixbuf(self.work_pixbuf)
|
||||||
|
|
||||||
|
def _fit_to_container(self):
|
||||||
|
self.fit_to_win = True
|
||||||
|
if self.work_pixbuf and self.pixbuf:
|
||||||
|
parent_aloc = self.get_parent().get_parent().get_allocation()
|
||||||
|
pw = parent_aloc.width
|
||||||
|
ph = parent_aloc.height
|
||||||
|
iw = self.pixbuf.get_width()
|
||||||
|
ih = self.pixbuf.get_height()
|
||||||
|
w = 0
|
||||||
|
h = 0
|
||||||
|
|
||||||
|
if iw == 0 or ih == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
w = pw;
|
||||||
|
h = ((ih * w) / iw + 0.5)
|
||||||
|
|
||||||
|
if h > ph:
|
||||||
|
h = ph
|
||||||
|
w = (iw * h) / ih + 0.5
|
||||||
|
|
||||||
|
|
||||||
|
self.work_pixbuf = self.pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
||||||
|
self.set_from_pixbuf(self.work_pixbuf)
|
||||||
|
|
||||||
|
def _horizontal_flip(self):
|
||||||
|
if self.work_pixbuf and self.pixbuf:
|
||||||
|
self.work_pixbuf = self.work_pixbuf.flip(False)
|
||||||
|
self.pixbuf = self.pixbuf.flip(False)
|
||||||
|
self.set_from_pixbuf(self.work_pixbuf)
|
||||||
|
|
||||||
|
def _rotate_right(self):
|
||||||
|
if self.work_pixbuf and self.pixbuf:
|
||||||
|
self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
|
||||||
|
self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
|
||||||
|
self.set_from_pixbuf(self.work_pixbuf)
|
||||||
|
|
||||||
|
def _zoom_in(self):
|
||||||
|
if self.work_pixbuf and self.pixbuf:
|
||||||
|
# TODO: Setup scale factor setting to pull from settings...
|
||||||
|
stepx = self.work_pixbuf.get_width() * 0.05
|
||||||
|
stepy = self.work_pixbuf.get_height() * 0.05
|
||||||
|
|
||||||
|
w = self.work_pixbuf.get_width() + stepx
|
||||||
|
h = self.work_pixbuf.get_height() + stepy
|
||||||
|
|
||||||
|
self.work_pixbuf = self.pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
||||||
|
self.set_from_pixbuf(self.work_pixbuf)
|
||||||
|
|
||||||
|
def _size_allocate(self):
|
||||||
|
if self.fit_to_win:
|
||||||
|
self._fit_to_container()
|
Loading…
Reference in New Issue
Block a user