From ed7eeb97aeee1d1c2a04ec07691637ca2c750c83 Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 25 Aug 2023 11:19:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=81=9C=E6=AD=A2=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E6=97=B6=E8=8E=B7=E5=8F=96=E9=9F=B3=E9=87=8F=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E5=AF=BC=E8=87=B4=E7=A8=8B=E5=BA=8F=E5=87=BA=E9=94=99?= =?UTF-8?q?=E7=9A=84bug;=20=E5=B0=81=E9=9D=A2=E5=9B=BE=E6=8C=891:1?= =?UTF-8?q?=E8=A3=81=E5=89=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/image.py | 42 ++++++++++++++++++++++++++++++++++++++---- window.py | 6 +++--- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/ui/image.py b/ui/image.py index a1af905..13a1145 100644 --- a/ui/image.py +++ b/ui/image.py @@ -13,7 +13,7 @@ class ScaleImage(Gtk.Image): self.reset(filepath) - def reset(self, filepath): + def reset(self, filepath, crop = False): if type(filepath) == str: self.origin = GdkPixbuf.Pixbuf.new_from_file(filepath) else: @@ -24,11 +24,17 @@ class ScaleImage(Gtk.Image): self.width = self.origin.get_width() self.height = self.origin.get_height() + self.set_from_pixbuf(self.pixbuf) else: - self.pixbuf = self.origin.scale_simple(self.width, self.height, GdkPixbuf.InterpType.BILINEAR) + w = self.origin.get_width() + h = self.origin.get_height() + if crop and w != h: + size = min(self.width, self.height) + self.clip_resize(size) + else: + self.pixbuf = self.origin.scale_simple(self.width, self.height, GdkPixbuf.InterpType.BILINEAR) - self.set_from_pixbuf(self.pixbuf) - return self + return self def resize(self, width, height): @@ -38,6 +44,34 @@ class ScaleImage(Gtk.Image): self.set_from_pixbuf(self.pixbuf) return self + def clip_resize(self, size): + w = self.origin.get_width() + h = self.origin.get_height() + _size = min(w, h) # 按小边裁剪成正方形 + size = min(size, _size) # 缩放按最小值来 + self.width = size + self.height = size + + surface = cairo.ImageSurface(cairo.Format.ARGB32, w, h) + ctx = cairo.Context(surface) + Gdk.cairo_set_source_pixbuf(ctx, self.origin, 0, 0) + + ctx.line_to(0, 0) + ctx.line_to(_size, 0) + ctx.line_to(_size, _size) + ctx.line_to(0, _size) + + ctx.close_path() + + ctx.clip() + ctx.paint() + + pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, _size, _size) + + self.pixbuf = pixbuf.scale_simple(size, size, GdkPixbuf.InterpType.BILINEAR) + self.set_from_pixbuf(self.pixbuf) + + return self def set_radius(self, radius = 0): w = self.width diff --git a/window.py b/window.py index 3c729cd..c3ccdc7 100644 --- a/window.py +++ b/window.py @@ -72,7 +72,7 @@ class SonistWindow(Gtk.Window): album = ScaleImage(album_img) disk.resize(192, 192) - album.resize(128, 128).set_radius(64) + album.clip_resize(128).set_radius(64) handler.resize(48, 96) self.handler = handler @@ -279,7 +279,7 @@ class SonistWindow(Gtk.Window): if first: self.update_play_stat(played == 'play') - self.update_volume(int(self.stat.get('volume'))) + self.update_volume(int(self.stat.get('volume') or 100)) if self.stat.get('single') == '1': self.ctrl_box.toggle_mode_btn(mode = 'single') @@ -332,6 +332,6 @@ class SonistWindow(Gtk.Window): @idle def update_album(self, filepath = './usr/share/sonist/avatar.jpg'): self.set_background_image(filepath) - self.album.reset(filepath).set_radius(64) + self.album.reset(filepath, True).set_radius(64)