0

I have successfully created an animated gif but there are 2 problems - the resulting animated gif is gray scale and dithered. I have used variants on Robert King's code snippet found here

Programmatically generate video or animated GIF in Python?

The King code snippet uses writeGif from image2gif.py with the following modifications: (i) the lines accessing "palette" (around line 97 in my version of image2gif.py), as per stackoverflow Q&A's

        #palette = getheader(im)[1]
        palette = im.palette.getdata()[1]

(ii) I ALSO had to add some code to account for the type of image that I have, the shape is of the image is 4 which I interpret as RGBA and I added lines 3 and 4 to writeGif (around line 186 in my image2gif.py)

1 if len(im.shape)==3 and im.shape[2]==3:

2 im = Image.fromarray(im,'RGB').convert('P',dither=dither)

3 elif len(im.shape)==3 and im.shape[2]==4:

4 im = Image.fromarray(im,'RGBA').convert('P',dither=dither)

5 elif len(im.shape)==2:

6 im = Image.fromarray(im,'L').convert('P',dither=dither)

How do I get color correct?. I think I am not accessing and writing the palette properly. I don't see any discussion of this.

NOTE: I can create an animated gif with these png files using GifConstructionKit (windows) but I have thousands of animated gifs to create and would like a programmatic solution. (I don't have ffmpeg nor installed openCV, yet.)

paulj03

Community
  • 1
  • 1
paulj03
  • 1
  • 1

1 Answers1

0

Well, I solved my own problem. I finally understood the messages about imagemagick and convert.exe. I downloaded and installed it then I wrote a script to execute convert.exe using the os.system function.

import os
cmd = 'cd <directory with images>'
cmd2 = 'convert *.png movie.gif'
os.system(cmd)
os.system(cmd2)

I actually write everything to a bat file and then run from a cmd windows (Windows machine). It seems much much faster than from Python console.

paulj
  • 141
  • 1
  • 4