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?
By default, FFmpeg uses a generic 256-color palette for every GIF encoding and doesn't take into account the colors in the input video. However, we can use the palettegen and paletteuse filters of FFmpeg to generate a custom palette from the colors in our original video to create a higher-quality GIF.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With