Page 1 of 1

[Solved] Downsizing image with large areas of same color

Posted: 2018-11-25T19:40:13-07:00
by scolby33
Hello,

I have a large, high-quality image of a flag that is generated from a TikZ drawing, created like so:

Code: Select all

latexmk -pdf flag.tex
gs -dSAFER -dNOPAUSE -dBATCH -sDEVICE=png16m -sOutputFile=flag.png
https://i.imgur.com/nWAXYa4.png

My goal is to turn this into a small (154x87) GIF. So far I have

Code: Select all

magick flag.png -colors 4 pallet-flag.png
magick pallet-flag.png -resize 154x87 flag.gif
(The final use case of this will see the image scaled to several additional smaller dimensions: 104x59, 42x24, and 25x14.)

Investigation with -unique-colors reveals that the original flag.png already only has 4 colors, but running it through -colors 4 makes the image smaller, so maybe that's still a good thing? Regardless, I still see the problematic behavior either way.

Here's the output from that resize: https://i.imgur.com/RR7Llex.gif. If you zoom in, you can see what I feel is unacceptably high levels of dithering in the solid yellow and red fields. I understand the need for some dithering around the curves/circles, but can't understand why it's happening out in the middle of the solid, unchanging color.

Am I overreacting to this? What parameters can I try in order to reduce this effect? So far, I have messed with -filter, -distort, and -dither without success, although these were certainly not exhaustive tests.

Re: Downsizing image with large areas of same color

Posted: 2018-11-25T20:20:46-07:00
by fmw42
try disabling dithering

Code: Select all

magick flag.png +dither -colors 4 -resize 154x87 flag.gif
but why bother with -colors 4 if you know it is already 4 colors. What you should do is save your colormap as a 1D image of the colors

Code: Select all

magick flag.png -unique-colors colormap.gif
Then

Code: Select all

magick flag.png -resize 154x87 +dither -remap colormap.gif  flag.gif
What you resize, it creates new colors, so you want to remap those colors to the exact colors in your input.

You can do that in one command as (unix syntax)

Code: Select all

magick flag.png \
\( +clone -unique-colors +write mpr:cmap +delete \) \
-resize 154x87 +dither -remap mpr:cmap  flag.gif

Re: Downsizing image with large areas of same color

Posted: 2018-11-25T20:34:58-07:00
by scolby33
The latter solution does create an image with exactly those 4 colors, but comes out way too "blocky."

The first solution, however is perfect! For my own understanding, I have a question though: why does +dither before resizing still result in an image with "in-between" colors at the edges of the curves? Isn't that process called dithering? Does -resize re-enable dithering?

Re: Downsizing image with large areas of same color

Posted: 2018-11-26T07:23:25-07:00
by snibgo
Dithering is a process that distributes errors caused by reducing the number of colours. "-resize" doesn't do that, so the dither flag makes no difference.

(Perhaps you are confusing dithering with anti-alias.)

Re: Downsizing image with large areas of same color

Posted: 2018-11-26T11:18:08-07:00
by fmw42
Resize does pixel interpolation and so creates new colors after you have use +dither -colors 4.

Re: Downsizing image with large areas of same color

Posted: 2018-11-26T16:47:19-07:00
by scolby33
snibgo wrote: 2018-11-26T07:23:25-07:00 (Perhaps you are confusing dithering with anti-alias.)
Exactly that! Thanks for explaining the difference.