簡體   English   中英

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

[英]Python PIL - add text BEFORE image (on top of image NOT on 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')

您應該創建一個比原始圖像更大的圖像,將第一個圖像和文本粘貼到其上,如下所示:

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