From 86e9b3c6005190e0e5516a499f472137a68c475a Mon Sep 17 00:00:00 2001 From: "K1llM@n" Date: Sat, 3 Jun 2023 00:23:11 +0300 Subject: [PATCH] Aspect ratio for preview --- roop/ui.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/roop/ui.py b/roop/ui.py index e2c3c5b..8c42745 100644 --- a/roop/ui.py +++ b/roop/ui.py @@ -3,7 +3,12 @@ from PIL import Image, ImageTk class PreviewWindow: + def __init__(self, master): + self.preview_width = 600 + self.preview_height = 650 + + self.master = master self.window = tk.Toplevel(self.master) # Override close button @@ -19,6 +24,10 @@ class PreviewWindow: self.frame.pack_propagate(0) self.frame.pack(fill='both', side='left', expand='True') + # Preview image + self.img_label = tk.Label(self.frame) + self.img_label.pack(side='top') + # Bottom frame buttons_frame = tk.Frame(self.frame, background="#2d3436") buttons_frame.pack(fill='both', side='bottom') @@ -60,12 +69,21 @@ class PreviewWindow: def update(self, frame): if not self.visible: return - + img = Image.fromarray(frame) - img = img.resize((600, 650), Image.ANTIALIAS) + width, height = img.size + aspect_ratio = 1 + if width > height: + aspect_ratio = self.preview_width / width + else: + aspect_ratio = self.preview_height / height + img = img.resize( + ( + int(width * aspect_ratio), + int(height * aspect_ratio) + ), + Image.ANTIALIAS + ) photo_img = ImageTk.PhotoImage(img) - img_frame = tk.Frame(self.frame) - img_frame.place(x=0, y=0) - img_label = tk.Label(img_frame, image=photo_img) - img_label.image = photo_img - img_label.pack(side='top') \ No newline at end of file + self.img_label.configure(image=photo_img) + self.img_label.image = photo_img \ No newline at end of file