#🔒 How to center text in pillow?

68 messages · Page 1 of 1 (latest)

urban mural
#

Here is my code

from PIL import Image, ImageFont, ImageDraw

font = ImageFont.truetype('ClearSans-Bold.ttf', 800)

im = Image.open('grid.png')

draw = ImageDraw.Draw(im)

draw.text(
  tuple(int(i/2) for i in im.size), # top left position
  '8', # text
  (119, 110, 101), # text color
  font, # font
  'mm' # anchor
)

im.save('output.png')

here is how it looks like:

willow tigerBOT
#

@urban mural

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

urban mural
#

it works fine for left-to-right

#

but height wise, it should be higher up

#

6 squares above compared to 1.5 squares below it

cunning phoenix
urban mural
cunning phoenix
#

what about a g?

urban mural
cunning phoenix
#

what about a different font?

formal coyote
#

u need to resize the grid accordingly

urban mural
cunning phoenix
#

I think you need a specific way of measuring the glyph then

urban mural
#

how do I calculate this part here?

cunning phoenix
#

I believe there's some sort of bounding box method

#

get glyph height
get center of canvas
offset based on values

formal coyote
#
from PIL import Image, ImageFont, ImageDraw

font = ImageFont.truetype('ClearSans-Bold.ttf', 800)

im = Image.open('grid.png')

draw = ImageDraw.Draw(im)

draw.text(
  tuple(im.size[0]/2,0), # top left position
  '8', # text
  (119, 110, 101), # text color
  font, # font
  'mm' # anchor
)

im.save('output.png')``` can u try this
urban mural
#

sure

formal coyote
#

there should be a slight change

urban mural
#

oh wait

formal coyote
#

it starts from top

#

no padding

urban mural
#

typeerror

#
    tuple(im.size[0]/2,0), # top left position
TypeError: tuple expected at most 1 argument, got 2
cunning phoenix
#

remove the tuple()

#
draw.text(
  (im.size[0]/2,0), # top left position
  '8', # text
  (119, 110, 101), # text color
  font, # font
  'mm' # anchor
)
formal coyote
cunning phoenix
formal coyote
#

wot

urban mural
#

it focuses on this red point and centers on it

cunning phoenix
#

This article discusses how to measure the size of a glyph

urban mural
#

i mean this point

cunning phoenix
#

can you share this grid image here?

urban mural
#

uh u probably want the ttf file too?

cunning phoenix
#

I actually just downloaded it, haha

#

it's at least centered for "8"

#
from PIL import Image, ImageFont, ImageDraw

font = ImageFont.truetype('ClearSans-Bold.ttf', 800)

metric = font.getmetrics()
ascent, descent = font.getmetrics()
(width, baseline), (offset_x, offset_y) = font.font.getsize('8')

im = Image.open('grid.png')

draw = ImageDraw.Draw(im)
placement = (im.size[0]//2, offset_y)
draw.text(
  placement, # top left position
  '8', # text
  (119, 110, 101), # text color
  font, # font
  'mm' # anchor
)

im.show()
#

this is admittedly very cobbled together from the stack overflow code without much understanding

urban mural
#

it's almost perfect, 3.5 squares to 4

cunning phoenix
#

honestly could just be a coincidence then that the numbers I plugged in were vaguely close

#

What are you ultimately trying to create here?

#

Just a single glyph at a time?

urban mural
#

the numbers could be 2, or 4, or 8, or 16, or 32, or 64, or 128, or 256, up to 2048

cunning phoenix
#

ahh

#

you might need to hardcode some offset Y for this specific font then

#

or keep fiddling around

#

I'd take the easy path though

#

although if you need to start changing font sizes to fill in more numbers, that could get messy

urban mural
cunning phoenix
#

then you could have a list of their offsets too

urban mural
#

I think there is a line between hardcoding

cunning phoenix
#

52 seems to work really well for this font size

cunning phoenix
willow tigerBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.