diff --git a/python/rapidocr_onnxruntime/utils.py b/python/rapidocr_onnxruntime/utils.py index ef60ed204..66800af9e 100644 --- a/python/rapidocr_onnxruntime/utils.py +++ b/python/rapidocr_onnxruntime/utils.py @@ -141,23 +141,30 @@ def load_img(self, img: InputType) -> np.ndarray: if isinstance(img, (str, Path)): self.verify_exist(img) try: - img = np.array(Image.open(img)) + img = self.img_to_ndarray(Image.open(img)) except UnidentifiedImageError as e: raise LoadImageError(f"cannot identify image file {img}") from e return img if isinstance(img, bytes): - img = np.array(Image.open(BytesIO(img))) + img = self.img_to_ndarray(Image.open(BytesIO(img))) return img if isinstance(img, np.ndarray): return img if isinstance(img, Image.Image): - return np.array(img) + return self.img_to_ndarray(img) raise LoadImageError(f"{type(img)} is not supported!") + def img_to_ndarray(self, img: Image.Image) -> np.ndarray: + if img.mode == "1": + img = img.convert("L") + return np.array(img) + else: + return np.array(img) + def convert_img(self, img: np.ndarray, origin_img_type): if img.ndim == 2: return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) diff --git a/python/rapidocr_openvino/utils.py b/python/rapidocr_openvino/utils.py index c5e008161..bb73a7a10 100644 --- a/python/rapidocr_openvino/utils.py +++ b/python/rapidocr_openvino/utils.py @@ -68,23 +68,30 @@ def load_img(self, img: InputType) -> np.ndarray: if isinstance(img, (str, Path)): self.verify_exist(img) try: - img = np.array(Image.open(img)) + img = self.img_to_ndarray(Image.open(img)) except UnidentifiedImageError as e: raise LoadImageError(f"cannot identify image file {img}") from e return img if isinstance(img, bytes): - img = np.array(Image.open(BytesIO(img))) + img = self.img_to_ndarray(Image.open(BytesIO(img))) return img if isinstance(img, np.ndarray): return img if isinstance(img, Image.Image): - return np.array(img) + return self.img_to_ndarray(img) raise LoadImageError(f"{type(img)} is not supported!") + def img_to_ndarray(self, img: Image.Image) -> np.ndarray: + if img.mode == "1": + img = img.convert("L") + return np.array(img) + else: + return np.array(img) + def convert_img(self, img: np.ndarray, origin_img_type): if img.ndim == 2: return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) diff --git a/python/rapidocr_paddle/utils.py b/python/rapidocr_paddle/utils.py index 0f7ac32a3..7f54f568e 100644 --- a/python/rapidocr_paddle/utils.py +++ b/python/rapidocr_paddle/utils.py @@ -150,23 +150,30 @@ def load_img(self, img: InputType) -> np.ndarray: if isinstance(img, (str, Path)): self.verify_exist(img) try: - img = np.array(Image.open(img)) + img = self.img_to_ndarray(Image.open(img)) except UnidentifiedImageError as e: raise LoadImageError(f"cannot identify image file {img}") from e return img if isinstance(img, bytes): - img = np.array(Image.open(BytesIO(img))) + img = self.img_to_ndarray(Image.open(BytesIO(img))) return img if isinstance(img, np.ndarray): return img if isinstance(img, Image.Image): - return np.array(img) + return self.img_to_ndarray(img) raise LoadImageError(f"{type(img)} is not supported!") + def img_to_ndarray(self, img: Image.Image) -> np.ndarray: + if img.mode == "1": + img = img.convert("L") + return np.array(img) + else: + return np.array(img) + def convert_img(self, img: np.ndarray, origin_img_type): if img.ndim == 2: return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)