我尝试将文本添加到GIF
文件,但似乎搞砸了文件。当我现在打开它时,它看起来就像你在旧电视上发出白噪声时所得到的,但有些位仍显示原始GIF
的一部分。有人可以帮我调试吗?
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import numpy
import imageio
img = Image.open('test.gif')
sequence = []
frameNo = 0
while True:
try:
img.seek(frameNo)
except EOFError:
break
newimg = Image.new("RGB", img.size)
newimg.paste(img)
draw = ImageDraw.Draw(newimg);
draw = ImageDraw.Draw(newimg)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("arial.ttf", 30
)
# draw.text((x, y),"Sample Text",(r,g,b))
draw.text((0, 0),"Sample Text",(255,255,255),font=font)
open_cv_image = numpy.array(newimg)
sequence.append(open_cv_image)
frameNo += 1
imageio.mimsave('temp.gif', sequence);