Skip to content

How to text allign to center? #7914

Closed Answered by nulano
Tetrax-10 asked this question in Q&A
Discussion options

You must be logged in to vote

By default, text is aligned using the la text anchor, i.e. the provided point (text_x, text_y) is to the top-left of the drawn text. If you want to align text to the middle, you want to use the mm text anchor:

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

img = Image.new("RGB", (1024, 720), "black")
I1 = ImageDraw.Draw(img)
myFont = ImageFont.truetype("ariblk.ttf", 105)
text_x = (img.width) // 2
text_y = (img.height) // 2
I1.text((text_x, text_y), "Movies", font=myFont, fill=(255, 255, 255), anchor="mm")

img.show()

See the documentation for more information.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Tetrax-10
Comment options

Answer selected by Tetrax-10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
2 participants