5

I'm drawing some text on a black strip, and then pasting the result on top of a base image, using PIL. One criticality is having the text position perfectly in the center of the black strip.

I cater for that via the following code:

from PIL import Image, ImageFont, ImageDraw

background = Image.new('RGB', (strip_width, strip_height)) #creating the black strip
draw = ImageDraw.Draw(background)
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 16)
text_width, text_height = draw.textsize("Foooo Barrrr!")
position = ((strip_width-text_width)/2,(strip_height-text_height)/2)
draw.text(position,"Foooo Barrrr!",(255,255,255),font=font)
offset = (0,base_image_height/2)
base_image.paste(background,offset)

Notice how I'm setting position.

Now with all said and done, the result looks like so:

enter image description here

The text isn't precisely middled. It's slightly to the right and down. How do I improve my algorithm?

Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
  • What are the contents of `text`? "Foooo Barrrr!"?? Why are you using `text` in the call to draw.textsize, but a new string literal in the call to draw.text? – tiwo May 02 '17 at 05:24
  • draw.textsize doesn't know the font you're using. I think you should pass it as second parameter. Maybe you need to go back to the docs, or provide a minimal example. – tiwo May 02 '17 at 05:27
  • @tiwo: `text` was a typo, it's supposed to be `"Foooo Barrrr!"`. I've fixed it in the question. – Hassan Baig May 02 '17 at 06:11
  • Does this answer your question? [Center-/middle-align text with PIL?](https://stackoverflow.com/questions/1970807/center-middle-align-text-with-pil) – bfontaine Jan 06 '23 at 09:57

1 Answers1

11

Remember to pass your font to draw.textsize as a second parameter (and also make sure you are really using the same text and font arguments to draw.textsize and draw.text).

Here's what worked for me:

from PIL import Image, ImageFont, ImageDraw

def center_text(img, font, text, color=(255, 255, 255)):
    draw = ImageDraw.Draw(img)
    text_width, text_height = draw.textsize(text, font)
    position = ((strip_width-text_width)/2,(strip_height-text_height)/2)
    draw.text(position, text, color, font=font)
    return img

Usage:

strip_width, strip_height = 300, 50
text = "Foooo Barrrr!!"

background = Image.new('RGB', (strip_width, strip_height)) #creating the black strip
font = ImageFont.truetype("times", 24)
center_text(background, font, "Foooo Barrrr!")

Result:

fooobarrrr

tiwo
  • 3,238
  • 1
  • 20
  • 33