How to Get Higher Quality Gifs with Python and FFMPEG

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@makerhacks·
0.000 HBD
How to Get Higher Quality Gifs with Python and FFMPEG
![Screenshot from 2018-05-25 15-20-32.png](https://cdn.steemitimages.com/DQmSLGV4uakoJJkMPv18S7FhL3uESNYdG8zpvqvkHubnyTS/Screenshot%20from%202018-05-25%2015-20-32.png)

One of the things that lets a gif down is obvious dithering. We can mitigate this by pre-processing the palette, then using it in the subsequent gif to find the best match rather than overplay our dither hand.

Take our [previous Python code](https://steemit.com/python/@makerhacks/how-to-create-animated-gifs-with-python) and tweak it thus:


```
# output the result
os.system('ffmpeg -framerate 24 -i frames/%03d.png -c:v ffv1 -r 24 -y out.avi')
os.system('ffmpeg -y -i out.avi -vf palettegen palette.png')
os.system('ffmpeg -y -i out.avi -i palette.png -lavfi paletteuse out.gif')
```

What we do here is take the AVI and extract a palette, which will look like this:

![Screenshot from 2018-05-25 15-24-32.png](https://cdn.steemitimages.com/DQmW8kzzS1Jkwc2mT29CjuAEq9DWLTpu4V3vhpbWZtxWu9e/Screenshot%20from%202018-05-25%2015-24-32.png)

Then we use that palette to generate the gif.

You can see the difference here is subtle unless you zoom in:

![original.gif](https://cdn.steemitimages.com/DQmTUWkBZhdF4ctAUdXpmFnRz1YYfU6mZig1Cdyf2Lzg8WX/original.gif)
![out.gif](https://cdn.steemitimages.com/DQmRX2ycWgH3ZBC6dDcr4gS6apFAmKoEWVNoPK6yRMMAaZh/out.gif)

But! you might find the higher quality *palette* version is smaller in file size!

![Screenshot from 2018-05-25 15-32-12.png](https://cdn.steemitimages.com/DQmX6j4ReiJRjb41nRbGBhtC6UrpQNZRrhZVAoxRY7ywBbH/Screenshot%20from%202018-05-25%2015-32-12.png)

<hr>
https://cdn.steemitimages.com/0x0/https://cdn.discordapp.com/attachments/383256479056134146/446022370608676864/makerhacks.png
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,