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)