-
Hello, I may be having a problem due to my poor English, perhaps because I do not fully understand the new resources. I'm trying to write text on a picture with pillow in Python 3.12.4. I've tried everything about the path to the font file. The font file is in the same folder as my project and when I write the following code I see 10 empty boxes: from PIL import Image, ImageDraw, ImageFont
# get an image
with Image.open("user_photo.png").convert("RGBA") as base:
# make a blank image for the text, initialized to transparent text color
txt = Image.new("RGBA", base.size, (255, 255, 255, 0))
# get a font
fnt = ImageFont.truetype("arial.ttf", 40)
# get a drawing context
d = ImageDraw.Draw(txt)
# draw text, half opacity
d.text((10, 10), "Hello", font=fnt, fill=(255, 255, 255, 128))
# draw text, full opacity
d.text((10, 60), "World", font=fnt, fill=(255, 255, 255, 255))
out = Image.alpha_composite(base, txt)
out.show() Hello World. Arial.ttf is installed. I tried with different font, I also tried to give the full path of the font. When I ran it before, it did not give any output, error, log. Today when I tried this code I got this result. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Beta Was this translation helpful? Give feedback.
If start with one of our test images
and run your code (changing 'arial.ttf' to 'Arial.ttf') on my macOS machine, I get
So I don't think there's anything wrong with your code.
What operating system are you using? Perhaps your local copy of arial.ttf is unusual?