Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getbbox() update for Pillow>=10.0.1 #214

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/layoutparser/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,26 @@ def _draw_vertical_text(
Ref: https://github.com/Belval/TextRecognitionDataGenerator/blob/7f4c782c33993d2b6f712d01e86a2f342025f2df/trdg/computer_text_generator.py
"""

space_height = int(image_font.getsize(" ")[1] * space_width)

char_heights = [
image_font.getsize(c)[1] if c != " " else space_height for c in text
]
text_width = max([image_font.getsize(c)[0] for c in text])
space_bbox = image_font.getbbox(" ")
space_height = (space_bbox[3] - space_bbox[1]) * space_width

char_heights = []
for c in text:
if c != " ":
char_bbox = image_font.getbbox(c)
char_height = char_bbox[3] - char_bbox[1]
else:
char_height = space_height
char_heights.append(char_height)

max_width = 0
for c in text:
char_bbox = image_font.getbbox(c)
char_width = char_bbox[2] - char_bbox[0]
if char_width > max_width:
max_width = char_width

text_width = max_width
text_height = sum(char_heights) + character_spacing * len(text)

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

start_x, start_y = ele.coordinates[:2]
text_w, text_h = font_obj.getsize(text)
left, top, right, bottom = font_obj.getbbox(text)
text_w = right - left
text_h = bottom - top

text_box_object = Rectangle(
start_x, start_y, start_x + text_w, start_y + text_h
Expand Down