From eeeee4c94eaefac8b33315b28ed151334a64159f Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Sun, 24 Mar 2024 17:29:26 +0100 Subject: [PATCH] feat: automatic image croping to match aspect ratio --- client.py | 3 ++- client_details.py | 7 ++++--- imagetools.py | 23 ++++++++++++++++------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/client.py b/client.py index b2a9dbb..619302a 100644 --- a/client.py +++ b/client.py @@ -186,7 +186,8 @@ def create_main_window_tile(software): if software.get_thumbnail(): img = PIL.Image.open(software.get_thumbnail()) - img = img.resize((200, 300)) + img = imagetools.smart_resize(img, 200, 300) + #img = img.resize((200, 300)) else: img = PIL.Image.new('RGB', (200, 300)) diff --git a/client_details.py b/client_details.py index 28c57ba..544fa0c 100644 --- a/client_details.py +++ b/client_details.py @@ -1,6 +1,7 @@ import PIL import tkinter import customtkinter +import imagetools def show_large_picture(app, path): @@ -14,7 +15,7 @@ def show_large_picture(app, path): y = app.winfo_height() img = PIL.Image.open(path) - img = img.resize((x-2*30, y-2*30)) + img = imagetools.smart_resize(img, x-2*30, y-2*30) img = PIL.ImageTk.PhotoImage(img) large_image = customtkinter.CTkButton(app, text="", image=img, width=x-2*30, height=y-2*30, @@ -30,7 +31,7 @@ def create_details_page(app, software, backswitch_function): if software.get_thumbnail(): path = software.get_thumbnail() img = PIL.Image.open(path) - img = img.resize((500, 700)) + img = imagetools.smart_resize(img, 500, 700) else: img = PIL.Image.new('RGB', (500, 700)) path = None @@ -136,7 +137,7 @@ def create_details_page(app, software, backswitch_function): i = 0 for path in software.pictures[1:]: img = PIL.Image.open(path) - img = img.resize((180, 180)) + img = imagetools.smart_resize(img, 180, 180) img = PIL.ImageTk.PhotoImage(img) extra_pic_button = customtkinter.CTkButton(picture_frame, text="", image=img, command=lambda path=path: show_large_picture(app, path), hover_color="black", corner_radius=0,) diff --git a/imagetools.py b/imagetools.py index 73d1514..e047fbb 100644 --- a/imagetools.py +++ b/imagetools.py @@ -1,11 +1,20 @@ import PIL -def load_and_keep_aspect_rescale(path, target_x, target_y): - '''Load an image and resize it while keeping the aspect ratio (crop if it doesnt fit)''' +def smart_resize(img, height, width): + '''Crop to aspect ratio and then resize''' - img = PIL.Image.open(path) - # get size - # img.size() + current_height, current_width = img.size + target_ratio = height/width + current_ratio = current_height/current_width - img = img.resize((x-2*30, y-2*30)) - img = PIL.ImageTk.PhotoImage(img) \ No newline at end of file + if current_ratio > target_ratio: + target_crop_height = current_width*target_ratio + diff = current_height - target_crop_height + img = img.crop((diff/2, 0, current_height-diff/2, current_width)) + elif current_ratio < target_ratio: + target_crop_width = current_height/target_ratio + diff = current_width - target_crop_width + img = img.crop((0, diff/2, current_height, current_width-diff/2)) + + img = img.resize((height, width)) + return img \ No newline at end of file