MCPcopy Create free account
hub / github.com/Layout-Parser/layout-parser / _draw_vertical_text

Function _draw_vertical_text

src/layoutparser/visualization.py:52–86  ·  view source on GitHub ↗

Helper function to draw text vertically. Ref: https://github.com/Belval/TextRecognitionDataGenerator/blob/7f4c782c33993d2b6f712d01e86a2f342025f2df/trdg/computer_text_generator.py

(
    text,
    image_font,
    text_color,
    text_background_color,
    character_spacing=2,
    space_width=1,
)

Source from the content-addressed store, hash-verified

50
51
52def _draw_vertical_text(
53 text,
54 image_font,
55 text_color,
56 text_background_color,
57 character_spacing=2,
58 space_width=1,
59):
60 """Helper function to draw text vertically.
61 Ref: https://github.com/Belval/TextRecognitionDataGenerator/blob/7f4c782c33993d2b6f712d01e86a2f342025f2df/trdg/computer_text_generator.py
62 """
63
64 space_height = int(image_font.getsize(" ")[1] * space_width)
65
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])
70 text_height = sum(char_heights) + character_spacing * len(text)
71
72 txt_img = Image.new("RGBA", (text_width, text_height), color=text_background_color)
73 txt_mask = Image.new("RGBA", (text_width, text_height), color=text_background_color)
74
75 txt_img_draw = ImageDraw.Draw(txt_img)
76 txt_mask_draw = ImageDraw.Draw(txt_mask)
77
78 for i, c in enumerate(text):
79 txt_img_draw.text(
80 (0, sum(char_heights[0:i]) + i * character_spacing),
81 c,
82 fill=text_color,
83 font=image_font,
84 )
85
86 return txt_img.crop(txt_img.getbbox())
87
88
89def _calculate_default_box_width(canvas):

Callers 1

draw_textFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected