Skip to content

Commit

Permalink
Adapt to Pillow v9 and v10
Browse files Browse the repository at this point in the history
  • Loading branch information
SWHL committed Feb 27, 2024
1 parent 7ddd002 commit e55c3a2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
19 changes: 17 additions & 2 deletions python/rapidocr_onnxruntime/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,12 @@ def draw_ocr_box_txt(
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")
Expand Down Expand Up @@ -475,3 +475,18 @@ def get_box_height(box: List[List[float]]) -> float:
@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."
)
19 changes: 17 additions & 2 deletions python/rapidocr_openvino/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,12 @@ def draw_ocr_box_txt(
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")
Expand Down Expand Up @@ -401,3 +401,18 @@ def get_box_height(box: List[List[float]]) -> float:
@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."
)
19 changes: 17 additions & 2 deletions python/rapidocr_paddle/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,12 @@ def draw_ocr_box_txt(
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")
Expand Down Expand Up @@ -494,3 +494,18 @@ def get_box_height(box: List[List[float]]) -> float:
@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."
)
2 changes: 1 addition & 1 deletion python/requirements_ort.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ numpy>=1.19.5
six>=1.15.0
Shapely>=1.7.1
PyYAML
Pillow<=10.0.1
Pillow
2 changes: 1 addition & 1 deletion python/requirements_paddle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ numpy>=1.19.5
six>=1.15.0
Shapely>=1.7.1
PyYAML
Pillow<=10.0.1
Pillow
2 changes: 1 addition & 1 deletion python/requirements_vino.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ numpy>=1.19.5
six>=1.15.0
Shapely>=1.7.1
PyYAML
Pillow<=10.0.1
Pillow

0 comments on commit e55c3a2

Please sign in to comment.