2

I create a GIF in FFmpeg using a custom color palette. Since custom color palette significantly reduces the size of the GIF I want to keep it as restricted as possible.

The GIF I'm trying to convert has white and two shades of blue. I assume there are also transitional colors, like a mix of white and blue to make the logo smoother on the edges. I don't know how many colors in the palette I need to create a good looking GIF.

Is there a way to calculate color palette size based on main colors?

Here are two GIFs for comparison with 8 and 16 color palette accordingly. Can someone explain why 8 colors produces dithering, distorts colors and 16 does not?

8 color palette GIF

16 color palette GIF

  • 1
    You're converting your animation/video to a GIF format using FFmpeg, but what was the original format? What was used to create the original animation? You probably should export as GIF from there – Luciano Oct 28 at 16:28
1

The biggest reduction you can make in a gif size is reducing the number of frames and modifying the playing time of each.

  • Your original gif has more than 420 frames, many of them repeated
  • The size is approximately 290kb

  • This gif has 160 frames
  • Size: 139 kb

enter image description here

enter image description here

  • Thank you for your answer, but I'm interested in color palettes. I know that decreasing FPS will decrease the size. I made it as low as possible to preserve the smoothness of the animation, if it gets below 23 FPS - it looks laggy. – Peter May 30 at 17:31
  • 2
    @Peter seems that you dont understand the answer. GIF files have no FPS. What they have is a display time for each frame. So instead of showing each frame for the same time youn can show some frames longer and some shorter. This has a big impact on your size... So you dont necceserily need to cut the palette down for a smaller file. – joojaa Oct 27 at 19:20
1

This will be just a clumsy answer because I do not use FFmpeg. But the problem I am seeing is not that you have a board palette, but that the program is adjusting the colors from a palette to a color that is not included in that palette.

Some options to explore:

  1. Take a color picker and measure one of the two colors the program used on your gif and modify the solid color to match it.

  2. See if your program can prepare a specific palette for you, normally this is choosing an optimized palette or an adaptive one.

  3. See if you can turn off any dithering.

  4. There is a chance your original image already has those artifacts, one example I can think of is that you are using an MP4 file with too much compression and it actually shows some blocks in the blue zone. Then the program is choosing another color for them.

  5. If options 2 and 3 are not available try using another program.


But let me answer the border original question.

How to create a color palette to avoid dithering in GIF?

You have a total pallete of 256 colors, but you can not use them all.

You need to limit the colors so some of the colors can be used in aliasing things.

On the case, you posted you need a blue, a white and let's say that we save 4-6 more colors for aliasing between blue and white. We can use just 8 colors.

But when we add a third color two things can happen.

a. The new color, green, only has borders with the blue. We then need 8 colors for the aliasing.

b. The new color, green, has borders also with the white. In this case, we need an additional 16 colors for our palette for the two different aliasings.

So I would limit the palette of let's say 16 colors so you have 16 additional colors per color for transition with the other colors. The more complex the scene the less exact this aliasing will be, but you have a clear limit for you to work.

This is of course on a flat design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.