-
Am trying to make the text allign center but it doens't work as I expected. Expected output: https://imgur.com/5HU7TBv.jpg (photoshop) My output: https://i.imgur.com/2jpgNr6.png (python code) from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageEnhance
# Open an Image and resize
img = Image.open("input.jpg")
# Calculate width to maintain aspect ratio for 720p height
original_width, original_height = img.size
new_height = 720
new_width = int((original_width / original_height) * new_height)
img = img.resize((new_width, new_height), Image.LANCZOS) # Use Image.LANCZOS for antialiasing
# Lower brightness to 50%
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(0.5) # 0.5 means 50% brightness
# Call draw Method to add 2D graphics in an image
I1 = ImageDraw.Draw(img)
# Custom font style and font size
myFont = ImageFont.truetype("Fact-Bold.ttf", 105)
# Calculate text position to center it
text_x = (img.width) // 2
text_y = (img.height) // 2
# Add Text to an image
I1.text((text_x, text_y), "Movies", font=myFont, fill=(255, 255, 255))
# Display edited image
img.show() |
Beta Was this translation helpful? Give feedback.
Answered by
nulano
Mar 29, 2024
Replies: 1 comment 1 reply
-
By default, text is aligned using the 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. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Tetrax-10
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 themm
text anchor:See the documentation for more information.