Home > Software design >  How to set anchor to fit the text at center of image with PIL.ImageDraw.Draw.text?
How to set anchor to fit the text at center of image with PIL.ImageDraw.Draw.text?

Time:02-21

Drawn text on an image with Pillow library, tried to fit the text anchored at center of image by option anchor='mm', but it looks not exactly as center of image.

Demo code

from PIL import Image, ImageDraw, ImageFont

im = Image.new("RGBA", (500, 500), (255, 255, 255, 255))
font = ImageFont.truetype(font='arial.ttf', size=320)
draw = ImageDraw.Draw(im)
draw.text((250, 250), "123", font=font, fill='black', anchor='mm')
im.show()

result:

enter image description here

expectation:

enter image description here

CodePudding user response:

The reason why the text looks misaligned is because there is a little margin at the left of the 1 and pillow will align the text including this margin.

Adding 0 at both ends will visualize that it is correctly centered and that there is a large margin at 1.

Image with 0 on both ends

If you want to align the text excluding margins, ImageFont.getmask is helpful.

def get_offset_for_true_mm(text, draw, font):
    anchor_bbox = draw.textbbox((0, 0), text, font=font, anchor='lt')
    anchor_center = (anchor_bbox[0]   anchor_bbox[2]) // 2, (anchor_bbox[1]   anchor_bbox[3]) // 2
    mask_bbox = font.getmask(text).getbbox()
    mask_center = (mask_bbox[0]   mask_bbox[2]) // 2, (mask_bbox[1]   mask_bbox[3]) // 2
    return anchor_center[0] - mask_center[0], anchor_center[1] - mask_center[1]


im = Image.new("RGBA", (500, 500), (255, 255, 255, 255))
font = ImageFont.truetype(font='arial.ttf', size=320)
draw = ImageDraw.Draw(im)
text = "123"
offset = get_offset_for_true_mm(text, draw, font)
draw.text((250   offset[0], 250   offset[1]), text, font=font, fill='black', anchor='mm')
im.show()

Result:

Image aligned excluding margins

  • Related