From c63bf607f9b451ab6b17e6b054c77f83048967b5 Mon Sep 17 00:00:00 2001 From: chflame163 Date: Tue, 17 Dec 2024 21:02:35 +0800 Subject: [PATCH] fix bug of function mask_area --- py/imagefunc.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/py/imagefunc.py b/py/imagefunc.py index 04513ac..304655b 100644 --- a/py/imagefunc.py +++ b/py/imagefunc.py @@ -1718,10 +1718,13 @@ def mask_area(image:Image) -> tuple: gray = cv2.cvtColor(cv2_image, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 127, 255, 0) locs = np.where(thresh == 255) - x1 = np.min(locs[1]) if len(locs[1]) > 0 else 0 - x2 = np.max(locs[1]) if len(locs[1]) > 0 else image.width - y1 = np.min(locs[0]) if len(locs[0]) > 0 else 0 - y2 = np.max(locs[0]) if len(locs[0]) > 0 else image.height + try: + x1 = np.min(locs[1]) + x2 = np.max(locs[1]) + y1 = np.min(locs[0]) + y2 = np.max(locs[0]) + except ValueError: + x1, y1, x2, y2 = -1, -1, 0, 0 x1, y1, x2, y2 = min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2) return (x1, y1, x2 - x1, y2 - y1)