To align PIL text to the center, you can use the textwidth
and textheight
attributes of the font to calculate the position of the text. Here's an example:
from PIL import Image, ImageDraw, ImageFont
# Create a blank image
image = Image.new('RGB', (500, 500), 'white')
# Get a font
font = ImageFont.truetype('arial.ttf', 36)
# Get the size of the text
text = 'Hello, world!'
textwidth, textheight = draw.textsize(text, font)
# Calculate the position of the text
x = (image.width - textwidth) / 2
y = (image.height - textheight) / 2
# Draw the text
draw = ImageDraw.Draw(image)
draw.text((x, y), text, font=font, fill='black')
# Show the image
image.show()
This will create an image with the text "Hello, world!" centered horizontally and vertically.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-03-19 11:00:00 +0000
Seen: 13 times
Last updated: Sep 20 '22
What is the method for aligning an image at the center using NPOI in c#?
What is the method to center a div vertically?
What is the method to ensure an image remains between two <br> tags regardless of the screen size?
What is the method for setting a dynamic background image in Vue?
What is the method to create circular thumbnails using PIL?
What is the method for placing an element at the center below its preceding sibling element?
How do I position the Widget in the middle of the body using tabBarView()?
What is the process for creating a circle using osgEarth::CircleNode?
How can I center a window when using PySide6, QT, QML, and WSL2?
How can a tangent line be drawn from a specified point to a circle using OpenCV?