[英]How could I add text to an image using PIL?
I've seen many questions and answers here, but they're not for Python.我在这里看到了很多问题和答案,但它们不适用于 Python。 I'm trying to make a meme generator, and I have a folder full of emme templates.
我正在尝试制作一个 meme 生成器,并且我有一个充满 emme 模板的文件夹。 I want to make the user get a meme template, view it then add text.
我想让用户获得一个模因模板,查看它然后添加文本。 Here is my code so far:
到目前为止,这是我的代码:
from PIL import Image
im = Image.open("Two-Buttons.jpg")
im.show()
Yeah, I know it's not much but I need help with getting user input on a certain part of the text.是的,我知道这并不多,但我需要帮助来获取用户对文本特定部分的输入。
First, you would need to import the necessary objects to draw on images from the PIL
library:首先,您需要导入必要的对象以从
PIL
库中绘制图像:
from PIL import Image, ImageDraw, ImageFont
Then, you instantiate the ImageDraw
class:然后,实例化
ImageDraw
class:
draw = ImageDraw.Draw(im)
Then you can simply do the following to draw on the original image im
:然后您可以简单地执行以下操作来绘制原始图像
im
:
text = 'Insert meme here'
font_type = ImageFont.truetype("arial.ttf", 18) # 18 -> font size
draw.text((100, 100), text, (255, 255, 0), font=font_type) # "draw.text(LOCATION, TEXT, COLOR, FONT_TYPE)"
im.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.