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

1

9

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?

Camelopardalis answered 13/11, 2019 at 7:19 Comment(0)
C
22

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

Camelopardalis answered 13/11, 2019 at 7:19 Comment(2)
This produces "Thread message queue blocking" error messages in version git-2020-02-09-5ad1c1a , rather confusingly. Was working at one time, suspect my frames are too big now.Benkley
@ConradB Yeah I get that message sometimes too. I don't think it will affect the output though. But you can often fix it by manually setting the thread queue size with the -thread_queue_size option, e.g., ffmpeg -thread_queue_size 1024 -i image_%02d.png -i palette.png -lavfi paletteuse video.gif.Camelopardalis

© 2022 - 2024 — McMap. All rights reserved.