I'm trying to take a .GIF
file, open it with PIL
, and write a text on it frame by frame. However, the code only saves an image (1 frame;it doesn't move like a .GIF
file.
The code:
import giphypop
from urllib import request
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
g = giphypop.Giphy()
img = g.translate("dog")
request.urlretrieve(img.media_url, "test.gif")
opened_gif = Image.open("test.gif")
opened_gif.load()
opened_gif.seek(1)
try:
while 1:
slide = opened_gif.seek(opened_gif.tell()+1)
draw = ImageDraw.Draw(slide)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("sans-serif.ttf", 16)
# draw.text((x, y),"Sample Text",(r,g,b))
draw.text((0, 0),"Sample Text",(255,255,255),font=font)
except EOFError:
pass # end of sequence
except AttributeError:
print("Couldn't use this slide")
opened_gif.save('test_with_caption.gif')