Trevor
Trevor

Reputation: 575

colors messed up (distorted) when making a gif from png files using ffmpeg

I have a sequence of png images: image_00.png, image_01.png, image_02.png, etc. I want to convert them to a gif, so I tried the command

ffmpeg -i image_%02d.png video.gif

Unfortunately, the resulting gif has distorted colors. More specifically, it added a weird sort of yellow haze around some objects in the video.

I also tried using the command above with all possible pixel format options (which I determined using the command ffmpeg -h encoder=gif): rgb8, bgr8, rgb4_byte, bgr4_byte, gray, pal8. For example ffmpeg -i image_%02d.png -pix_fmt rgb8 video.gif. Unfortunately, all of the resulting gifs had some sort of color distortion.

I also observed that this distortion does not occur if I convert the images to mp4 instead of gif. However, if I try converting that mp4 to a gif, I end up with the distortion again.

How can I produce this gif without color distortion?

Upvotes: 9

Views: 7385

Answers (1)

Trevor
Trevor

Reputation: 575

I was able to fix this problem by first generating a palette for all of the png images. I then used that palette to create the gif:

ffmpeg -i image_%02d.png -vf palettegen palette.png
ffmpeg -i image_%02d.png -i palette.png -lavfi paletteuse video.gif

For more info, see the "palettegen" and "paletteuse" sections of https://ffmpeg.org/ffmpeg-filters.html

Upvotes: 22

Related Questions