0

I tried adding text to a GIF file but it seems to have messed up the file. When I now open it, it looks like what you get when you have white noise on an old TV, but with some bits still showing part of the original GIF. Could someone help me debug?

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);
whatwhatwhat
  • 1,991
  • 4
  • 31
  • 50
  • you have to `getpalette()` from first frame and `setpalette()` with all other frames. You can see resolve in some other question. – furas Jan 05 '17 at 04:43
  • http://stackoverflow.com/questions/35613716/imagemagick-pillow-generate-malformed-gif-frames/35615319#35615319 – furas Jan 05 '17 at 04:47

0 Answers0