From 9b29f7b274d8553ade36313b6ff4ed271c35b9b3 Mon Sep 17 00:00:00 2001 From: Olli Date: Sat, 10 Jan 2026 16:22:03 +0100 Subject: [PATCH] Preserve aspect ratio of album covers in grid views (close #111) --- src/librarypanel.py | 12 +++--------- src/playlistpanel.py | 12 +++--------- src/utils.py | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/librarypanel.py b/src/librarypanel.py index 22a8b43..02d8c08 100644 --- a/src/librarypanel.py +++ b/src/librarypanel.py @@ -444,15 +444,9 @@ class LibraryPanel(Adw.Bin): self.standalone_image.set_pixel_size(min(size_width, size_height)/2) return - # Skalierungswert für Breite und Höhe ermitteln - ratio_w = float(size_width) / float(pixbuf.get_width()) - ratio_h = float(size_height) / float(pixbuf.get_height()) - # Kleineren beider Skalierungswerte nehmen, nicht Hochskalieren - ratio = min(ratio_w, ratio_h) - ratio = min(ratio, 1) - # Neue Breite und Höhe berechnen - width = int(math.floor(pixbuf.get_width() * ratio)) - height = int(math.floor(pixbuf.get_height() * ratio)) + (width, height) = Utils.calculate_size(pixbuf.get_width(), + pixbuf.get_height(), size_width, + size_height) if width <= 0 or height <= 0: return # Pixelpuffer auf Oberfläche zeichnen diff --git a/src/playlistpanel.py b/src/playlistpanel.py index 3c18e9c..9f59e93 100644 --- a/src/playlistpanel.py +++ b/src/playlistpanel.py @@ -252,15 +252,9 @@ class PlaylistPanel(Adw.Bin): self.standalone_image.set_pixel_size(min(size_width, size_height)/2) return - # Skalierungswert für Breite und Höhe ermitteln - ratio_w = float(size_width) / float(pixbuf.get_width()) - ratio_h = float(size_height) / float(pixbuf.get_height()) - # Kleineren beider Skalierungswerte nehmen, nicht Hochskalieren - ratio = min(ratio_w, ratio_h) - ratio = min(ratio, 1) - # Neue Breite und Höhe berechnen - width = int(math.floor(pixbuf.get_width() * ratio)) - height = int(math.floor(pixbuf.get_height() * ratio)) + (width, height) = Utils.calculate_size(pixbuf.get_width(), + pixbuf.get_height(), size_width, + size_height) if width <= 0 or height <= 0: return # Pixelpuffer auf Oberfläche zeichnen diff --git a/src/utils.py b/src/utils.py index 4b58fb9..544e415 100644 --- a/src/utils.py +++ b/src/utils.py @@ -2,6 +2,7 @@ import gi import hashlib +import math import locale import logging import os @@ -36,7 +37,10 @@ class Utils: if albumart: pixbuf = Utils.load_pixbuf(albumart) if pixbuf is not None: - pixbuf = pixbuf.scale_simple(size, size, + (width, height) = Utils.calculate_size(pixbuf.get_width(), + pixbuf.get_height(), + size, size) + pixbuf = pixbuf.scale_simple(width, height, GdkPixbuf.InterpType.HYPER) try: pixbuf.savev(cache_url, 'jpeg', [], []) @@ -78,6 +82,19 @@ class Utils: m.update(value.encode('utf-8')) return m.hexdigest() + @staticmethod + def calculate_size(src_width, src_height, dest_width, dest_height): + ratio_w = float(dest_width) / float(src_width) + ratio_h = float(dest_height) / float(src_height) + ratio = min(min(ratio_w, ratio_h), 1) + if ratio == 1: + return (src_width, src_height) + + width = int(math.floor(src_width * ratio)) + height = int(math.floor(src_height * ratio)) + + return (width, height) + class SortOrder: ARTIST = 0