-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
Description
I'm placing some text on an image as follows, using Python 3.7.3 and PIL 5.4.1:
from PIL import Image, ImageDraw, ImageFont
image = ...
font_filepath = ...
font_size = ...
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(font_filepath, font_size)
xy = ... # generate random (x, y) coordinates to place the text at
text = ...
draw.text(xy, text, font=font)I want to get the character-level bounding boxes around the text placed on the image. For example, if text = "hello", I want a list of five rectangles, each of which bounds a corresponding letter in "hello". For example, the bounding box for "l" should be thinner and taller than the bounding box for "o", and the bounding boxes for the two "l"s should have different x-positions and same y-positions.
I have investigated using:
size = font.getsize(text)
mask = font.getmask(text)However, I don't know how to interpret mask, because:
maskis anImagingCoreobject instead of anImageobjectlen(mask)does not even equal the area calculated bysize[0] * size[1]
What is the easiest way to obtain character-level bounding boxes for text placed on an image by PIL?
mostafahadian and schneiderfelipe