简体   繁体   English

Python PIL - 在图像之前添加文本(在图像顶部而不是在图像上)

[英]Python PIL - add text BEFORE image (on top of image NOT on the image)

How can I add text on top of an image (ie NOT INSIDE the image)?如何在图像顶部添加文本(即不在图像内部)?

看例子

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw 

img = Image.open("SAMPLE-IN.png")
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("FONTS/arial.ttf", 36)
# draw.text((x, y),"Sample Text",(r,g,b))
draw.text((0,0),"Sample Text",(0,255,255),font=font)
img.save('sample-out.jpg')

You should create an image that is bigger than the original one, paste the first one and the text onto it, like this:您应该创建一个比原始图像更大的图像,将第一个图像和文本粘贴到其上,如下所示:

from PIL import Image, ImageFont, ImageDraw, ImageOps
img = Image.open("SAMPLE-IN.png")
img = ImageOps.expand(img, border=10, fill=(255,255,255))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("FONTS/arial.ttf", 36)
draw.text((0,0),"Sample Text",(0,255,255),font=font)
img.save('sample-out.jpg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM