From 23f504acf7a0d549e19f13216891bbf99d54ab0c Mon Sep 17 00:00:00 2001 From: Pancham Agarwal <70520879+Pancham1603@users.noreply.github.com> Date: Mon, 29 Jul 2024 12:47:33 +0530 Subject: [PATCH] getbbox() update for Pillow>=10.0.1 --- src/layoutparser/visualization.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/layoutparser/visualization.py b/src/layoutparser/visualization.py index 002d063..8a0165b 100644 --- a/src/layoutparser/visualization.py +++ b/src/layoutparser/visualization.py @@ -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) @@ -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