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

后端 未结 3 887
北海茫月
北海茫月 2021-02-04 12:38

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

3条回答
  •  甜味超标
    2021-02-04 13:24

    PIL can do it:

    from PIL import Image, ImageFont, ImageDraw
    font = ImageFont.truetype("/usr/share/fonts/dejavu/DejaVuSans.ttf", 25)
    img = Image.new("RGBA", (200,200), (120,20,20))
    draw = ImageDraw.Draw(img)
    draw.text((0,0), "This is a test", (255,255,0), font=font)
    img.save("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)
    

    Source: http://python-catalin.blogspot.com/2010/06/add-text-on-image-with-pil-module.html

提交回复
热议问题