added webp support, cleaned up configs

This commit is contained in:
itdominator 2023-02-21 16:26:04 -06:00
parent 058a5a7ef6
commit 4c4cb051d8
2 changed files with 47 additions and 3 deletions

View File

@ -13,6 +13,12 @@ from .controller_data import Controller_Data
from .mixins.tree_view_update_mixin import TreeViewUpdateMixin
try:
from PIL import Image as PImage
except Exception as e:
PImage = None
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
@ -119,8 +125,25 @@ class Controller(TreeViewUpdateMixin, Controller_Data):
geom_rec = self.image_area.get_parent().get_parent().get_allocated_size()[0]
width = geom_rec.width - 15
height = geom_rec.height - 15
wxh = [width, height]
self.image_area.set_size_request(width, height)
return GdkPixbuf.Pixbuf.new_from_file_at_scale(uri, width, height, True)
if PImage and uri.lower().endswith(".webp"):
return self.image2pixbuf(uri, wxh)
else:
return GdkPixbuf.Pixbuf.new_from_file_at_scale(uri, width, height, True)
def image2pixbuf(self, path, wxh):
"""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(wxh[0], wxh[1], 2) # BILINEAR = 2
def _load_image(self):
self.clear_children(self.image_area)

View File

@ -5,13 +5,19 @@ from os.path import isfile
# Gtk imports
import gi
gi.require_version('GdkPixbuf', '2.0')
from gi.repository import GdkPixbuf
from gi.repository import GdkPixbuf, GLib
# Application imports
from .mixins.desktopiconmixin import DesktopIconMixin
from .mixins.videoiconmixin import VideoIconMixin
try:
from PIL import Image as PImage
except Exception as e:
PImage = None
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
@ -60,12 +66,27 @@ class Icon(DesktopIconMixin, VideoIconMixin):
.get_static_image() \
.scale_simple(wxh[0], wxh[1], GdkPixbuf.InterpType.BILINEAR)
else:
return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], True)
if PImage and path.lower().endswith(".webp"):
return self.image2pixbuf(path, wxh)
else:
return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], True)
except Exception as e:
print("Image Scaling Issue:")
print( repr(e) )
return None
def image2pixbuf(self, path, wxh):
"""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(wxh[0], wxh[1], 2) # BILINEAR = 2
def create_from_file(self, path):
try:
return GdkPixbuf.Pixbuf.new_from_file(path)