[英]Moving text lower one image at a time in a loop, using PIL
我是PIL的新手。 我試圖將多個圖像保存為一個循環,以便更改每個圖像中文本的位置。
這是我的代碼:
from PIL import Image, ImageDraw, ImageFont
import os
files = []
C = 0
base = Image.open('car.jpg').convert('RGBA')
txt = Image.new('RGBA', base.size, (255,255,255,0))
fnt = ImageFont.truetype('calibrib.ttf', 40)
d = ImageDraw.Draw(txt)
W = 0
while C < 175:
d.text((0,W), "Test Text", font=fnt, fill=(255,255,255,255))
out = Image.alpha_composite(base, txt)
f = (3-len(str(C)))*'0'+str(C)
folder = os.getcwd()
out.save(folder + '/images/a%s.png' % f, "PNG")
files.append('a%s.png' % f)
W = W+1
C = C+1
我想要的輸出是看到“測試文本”在最后一張圖像中垂直居中。
文本應在循環中一次向下移動一幅圖像。
ImageDraw.Draw調用使txt成為要在其上繪制的圖像,每次調用d.text時,您都將在txt圖像上繪制新文本,而不會從上次迭代中刪除先前的文本。 要解決此問題,您需要在每次迭代時重置txt對象。 您可以通過致電
txt = Image.new('RGBA', base.size, (255,255,255,0))
d = ImageDraw.Draw(txt)
在while循環中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.