Skip to content

πŸ“„ Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

Bug fix for converting error when processing bitmap image. (#171)

HAL9000COMf602d301e549
diff --git a/python/rapidocr_onnxruntime/utils.py b/python/rapidocr_onnxruntime/utils.py
index ef60ed2..66800af 100644
--- a/python/rapidocr_onnxruntime/utils.py
+++ b/python/rapidocr_onnxruntime/utils.py
@@ -141,23 +141,30 @@ class LoadImage:
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 c5e0081..bb73a7a 100644
--- a/python/rapidocr_openvino/utils.py
+++ b/python/rapidocr_openvino/utils.py
@@ -68,23 +68,30 @@ class LoadImage:
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 0f7ac32..7f54f56 100644
--- a/python/rapidocr_paddle/utils.py
+++ b/python/rapidocr_paddle/utils.py
@@ -150,23 +150,30 @@ class LoadImage:
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)