Merge pull request #911 from nimishgautam/main

Fix cv2 size errors on first run in ui.py
pull/932/head
Kenneth Estanislao 2025-02-05 12:51:39 +08:00 committed by GitHub
commit 28c4b34db1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 8 deletions

View File

@ -696,17 +696,21 @@ def check_and_ignore_nsfw(target, destroy: Callable = None) -> bool:
def fit_image_to_size(image, width: int, height: int): 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 return image
h, w, _ = image.shape h, w, _ = image.shape
ratio_h = 0.0 ratio_h = 0.0
ratio_w = 0.0 ratio_w = 0.0
if width > height:
ratio_h = height / h
else:
ratio_w = width / w ratio_w = width / w
ratio = max(ratio_w, ratio_h) ratio_h = height / h
new_size = (int(ratio * w), int(ratio * 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) return cv2.resize(image, dsize=new_size)