From 75b87f301901092d488105dec749c03877d81bf4 Mon Sep 17 00:00:00 2001 From: Antoine Buchser <10513467+AntwaneB@users.noreply.github.com> Date: Mon, 5 Jun 2023 19:34:38 +0100 Subject: [PATCH] Simplified the way to maintain aspect ratio of the preview, and maintaining aspect ratio of the miniatures --- roop/ui.py | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/roop/ui.py b/roop/ui.py index d8891fb..bedc4d4 100644 --- a/roop/ui.py +++ b/roop/ui.py @@ -1,6 +1,6 @@ import tkinter as tk from typing import Any, Callable, Tuple -from PIL import Image, ImageTk +from PIL import Image, ImageTk, ImageOps import webbrowser from tkinter import filedialog from tkinter.filedialog import asksaveasfilename @@ -69,19 +69,7 @@ def init_slider(frames_count, change_handler): def update_preview(frame): img = Image.fromarray(frame) - width, height = img.size - aspect_ratio = 1 - if width > height: - aspect_ratio = max_preview_size / width - else: - aspect_ratio = max_preview_size / height - img = img.resize( - ( - int(width * aspect_ratio), - int(height * aspect_ratio) - ), - Image.ANTIALIAS - ) + img = ImageOps.contain(img, (max_preview_size, max_preview_size), Image.LANCZOS) photo_img = ImageTk.PhotoImage(img) preview_image_frame.configure(image=photo_img) preview_image_frame.image = photo_img @@ -211,7 +199,7 @@ def open_preview_window(get_video_frame, target_path): def preview_face(path): img = Image.open(path) - img = img.resize((180, 180), Image.ANTIALIAS) + img = ImageOps.contain(img, (180, 180), Image.LANCZOS) photo_img = ImageTk.PhotoImage(img) face_label.configure(image=photo_img) face_label.image = photo_img @@ -219,7 +207,7 @@ def preview_face(path): def preview_target(frame): img = Image.fromarray(frame) - img = img.resize((180, 180), Image.ANTIALIAS) + img = ImageOps.contain(img, (180, 180), Image.LANCZOS) photo_img = ImageTk.PhotoImage(img) target_label.configure(image=photo_img) target_label.image = photo_img @@ -312,4 +300,4 @@ def init( status_label = tk.Label(window, width=580, justify="center", text="Status: waiting for input...", fg="#2ecc71", bg="#2d3436") status_label.place(x=10,y=640,width=580,height=30) - return window \ No newline at end of file + return window