Skip to content

Commit e27ced6

Browse files
IgorTavcarclaude
andcommitted
Replace deprecated getsize() with getbbox() for Pillow>=10.0.1
getsize() was removed in newer Pillow versions. Uses getbbox() which returns (left, top, right, bottom) to compute text dimensions instead. From: Layout-Parser#214 (Pancham1603) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c453d3d commit e27ced6

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

src/layoutparser/visualization.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,26 @@ def _draw_vertical_text(
6161
Ref: https://github.com/Belval/TextRecognitionDataGenerator/blob/7f4c782c33993d2b6f712d01e86a2f342025f2df/trdg/computer_text_generator.py
6262
"""
6363

64-
space_height = int(image_font.getsize(" ")[1] * space_width)
64+
space_bbox = image_font.getbbox(" ")
65+
space_height = (space_bbox[3] - space_bbox[1]) * space_width
66+
67+
char_heights = []
68+
for c in text:
69+
if c != " ":
70+
char_bbox = image_font.getbbox(c)
71+
char_height = char_bbox[3] - char_bbox[1]
72+
else:
73+
char_height = space_height
74+
char_heights.append(char_height)
75+
76+
max_width = 0
77+
for c in text:
78+
char_bbox = image_font.getbbox(c)
79+
char_width = char_bbox[2] - char_bbox[0]
80+
if char_width > max_width:
81+
max_width = char_width
6582

66-
char_heights = [
67-
image_font.getsize(c)[1] if c != " " else space_height for c in text
68-
]
69-
text_width = max([image_font.getsize(c)[0] for c in text])
83+
text_width = max_width
7084
text_height = sum(char_heights) + character_spacing * len(text)
7185

7286
txt_img = Image.new("RGBA", (text_width, text_height), color=text_background_color)
@@ -389,7 +403,9 @@ def draw_box(
389403
text = str(ele.type) if not text else text + ": " + str(ele.type)
390404

391405
start_x, start_y = ele.coordinates[:2]
392-
text_w, text_h = font_obj.getsize(text)
406+
left, top, right, bottom = font_obj.getbbox(text)
407+
text_w = right - left
408+
text_h = bottom - top
393409

394410
text_box_object = Rectangle(
395411
start_x, start_y, start_x + text_w, start_y + text_h

0 commit comments

Comments
 (0)