diff --git a/modules/ui.py b/modules/ui.py index b31cd8a..94fa5cf 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -696,17 +696,21 @@ def check_and_ignore_nsfw(target, destroy: Callable = None) -> bool: def fit_image_to_size(image, width: int, height: int): - if width is None and height is None: + if width is None or height is None or width <= 0 or height <= 0: return image h, w, _ = image.shape ratio_h = 0.0 ratio_w = 0.0 - if width > height: - ratio_h = height / h - else: - ratio_w = width / w - ratio = max(ratio_w, ratio_h) - new_size = (int(ratio * w), int(ratio * h)) + ratio_w = width / w + ratio_h = height / h + # Use the smaller ratio to ensure the image fits within the given dimensions + ratio = min(ratio_w, ratio_h) + + # Compute new dimensions, ensuring they're at least 1 pixel + new_width = max(1, int(ratio * w)) + new_height = max(1, int(ratio * h)) + new_size = (new_width, new_height) + return cv2.resize(image, dsize=new_size) @@ -1199,4 +1203,4 @@ def update_webcam_target( target_label_dict_live[button_num] = target_image else: update_pop_live_status("Face could not be detected in last upload!") - return map \ No newline at end of file + return map