redmarv redmarv - 3 months ago 15x
Python Question

How can I write text over an image, and overlay another image on it, in Python?

I need to put some text over a PNG image in Python, I need to put another image too over the first one.

So I'll have a base image (the same for every image created), a logo to put over it in the upper left corner, and a text all over the image (non-specific font, I just need to set the font size).

Could I use PIL, or another Library?

I searched over StackOverflow and Google too, but I could not find tips on how to do this.



PIL can do it:

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
font = ImageFont.truetype("/usr/share/fonts/dejavu/DejaVuSans.ttf", 25)
img ="RGBA", (200,200), (120,20,20))
draw = ImageDraw.Draw(img)
draw.text((0,0), "This is a test", (255,255,0), font=font)
draw = ImageDraw.Draw(img)"a_test.png")

The only error that can occur is not to find the font. In this case you must change the code line:

font = ImageFont.truetype("/usr/share/fonts/dejavu/DejaVuSans.ttf",25)