Skip to content

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

Adapt to Pillow v9 and v10

SWHLe55c3a2a3784
diff --git a/python/rapidocr_onnxruntime/utils.py b/python/rapidocr_onnxruntime/utils.py
index 1eb01e6..d56684f 100644
--- a/python/rapidocr_onnxruntime/utils.py
+++ b/python/rapidocr_onnxruntime/utils.py
@@ -434,12 +434,12 @@ class VisRes:
font_size = max(int(box_width * 0.9), 10)
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
cur_y = box[0][1]
+
for c in txt:
- char_size = font.getsize(c)
draw_right.text(
(box[0][0] + 3, cur_y), c, fill=(0, 0, 0), font=font
)
- cur_y += char_size[1]
+ cur_y += self.get_char_size(font, c)
else:
font_size = max(int(box_height * 0.8), 10)
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
@@ -475,3 +475,18 @@ class VisRes:
@staticmethod
def get_box_width(box: List[List[float]]) -> float:
return math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2)
+
+ @staticmethod
+ def get_char_size(font, char_str: str) -> float:
+ # compatible with Pillow v9 and v10.
+ if hasattr(font, "getsize"):
+ get_size_func = getattr(font, "getsize")
+ return get_size_func(char_str)[1]
+
+ if hasattr(font, "getlength"):
+ get_size_func = getattr(font, "getlength")
+ return get_size_func(char_str)
+
+ raise ValueError(
+ "The Pillow ImageFont instance has not getsize or getlength func."
+ )
diff --git a/python/rapidocr_openvino/utils.py b/python/rapidocr_openvino/utils.py
index 8ce9e86..2dcf149 100644
--- a/python/rapidocr_openvino/utils.py
+++ b/python/rapidocr_openvino/utils.py
@@ -360,12 +360,12 @@ class VisRes:
font_size = max(int(box_width * 0.9), 10)
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
cur_y = box[0][1]
+
for c in txt:
- char_size = font.getsize(c)
draw_right.text(
(box[0][0] + 3, cur_y), c, fill=(0, 0, 0), font=font
)
- cur_y += char_size[1]
+ cur_y += self.get_char_size(font, c)
else:
font_size = max(int(box_height * 0.8), 10)
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
@@ -401,3 +401,18 @@ class VisRes:
@staticmethod
def get_box_width(box: List[List[float]]) -> float:
return math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2)
+
+ @staticmethod
+ def get_char_size(font, char_str: str) -> float:
+ # compatible with Pillow v9 and v10.
+ if hasattr(font, "getsize"):
+ get_size_func = getattr(font, "getsize")
+ return get_size_func(char_str)[1]
+
+ if hasattr(font, "getlength"):
+ get_size_func = getattr(font, "getlength")
+ return get_size_func(char_str)
+
+ raise ValueError(
+ "The Pillow ImageFont instance has not getsize or getlength func."
+ )
diff --git a/python/rapidocr_paddle/utils.py b/python/rapidocr_paddle/utils.py
index 323884a..eda014c 100644
--- a/python/rapidocr_paddle/utils.py
+++ b/python/rapidocr_paddle/utils.py
@@ -453,12 +453,12 @@ class VisRes:
font_size = max(int(box_width * 0.9), 10)
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
cur_y = box[0][1]
+
for c in txt:
- char_size = font.getsize(c)
draw_right.text(
(box[0][0] + 3, cur_y), c, fill=(0, 0, 0), font=font
)
- cur_y += char_size[1]
+ cur_y += self.get_char_size(font, c)
else:
font_size = max(int(box_height * 0.8), 10)
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
@@ -494,3 +494,18 @@ class VisRes:
@staticmethod
def get_box_width(box: List[List[float]]) -> float:
return math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2)
+
+ @staticmethod
+ def get_char_size(font, char_str: str) -> float:
+ # compatible with Pillow v9 and v10.
+ if hasattr(font, "getsize"):
+ get_size_func = getattr(font, "getsize")
+ return get_size_func(char_str)[1]
+
+ if hasattr(font, "getlength"):
+ get_size_func = getattr(font, "getlength")
+ return get_size_func(char_str)
+
+ raise ValueError(
+ "The Pillow ImageFont instance has not getsize or getlength func."
+ )
diff --git a/python/requirements_ort.txt b/python/requirements_ort.txt
index 3794d83..4111d1f 100644
--- a/python/requirements_ort.txt
+++ b/python/requirements_ort.txt
@@ -5,4 +5,4 @@ numpy>=1.19.5
six>=1.15.0
Shapely>=1.7.1
PyYAML
-Pillow<=10.0.1
+Pillow
diff --git a/python/requirements_paddle.txt b/python/requirements_paddle.txt
index c163cda..1e81988 100644
--- a/python/requirements_paddle.txt
+++ b/python/requirements_paddle.txt
@@ -4,4 +4,4 @@ numpy>=1.19.5
six>=1.15.0
Shapely>=1.7.1
PyYAML
-Pillow<=10.0.1
\ No newline at end of file
+Pillow
\ No newline at end of file
diff --git a/python/requirements_vino.txt b/python/requirements_vino.txt
index 0b4f3e5..881bbd3 100644
--- a/python/requirements_vino.txt
+++ b/python/requirements_vino.txt
@@ -5,4 +5,4 @@ numpy>=1.19.5
six>=1.15.0
Shapely>=1.7.1
PyYAML
-Pillow<=10.0.1
\ No newline at end of file
+Pillow
\ No newline at end of file