如何使用Python在图像上书写文本,并在图像上覆盖另一个图像? [英] How can I write text over an image, and overlay another image on it, in Python?

查看:909
本文介绍了如何使用Python在图像上书写文本,并在图像上覆盖另一个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Python的PNG图像上放置一些文本,在第一个图像上也放置另一个图像.

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).

我可以使用PIL还是其他库?

Could I use PIL, or another Library?

我也在StackOverflow和Google上进行了搜索,但是找不到有关该操作的提示.

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

谢谢.

推荐答案

PIL可以做到:

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)
draw = ImageDraw.Draw(img)
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)

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

这篇关于如何使用Python在图像上书写文本,并在图像上覆盖另一个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆