[Solved] Downsizing image with large areas of same color

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
scolby33
Posts: 4
Joined: 2018-11-25T19:24:42-07:00
Authentication code: 1152

[Solved] Downsizing image with large areas of same color

Post by scolby33 » 2018-11-25T19:40:13-07:00

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.
Last edited by scolby33 on 2018-11-26T16:47:38-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Downsizing image with large areas of same color

Post by fmw42 » 2018-11-25T20:20:46-07:00

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

scolby33
Posts: 4
Joined: 2018-11-25T19:24:42-07:00
Authentication code: 1152

Re: Downsizing image with large areas of same color

Post by scolby33 » 2018-11-25T20:34:58-07:00

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?

snibgo
Posts: 11910
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Downsizing image with large areas of same color

Post by snibgo » 2018-11-26T07:23:25-07:00

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.)
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Downsizing image with large areas of same color

Post by fmw42 » 2018-11-26T11:18:08-07:00

Resize does pixel interpolation and so creates new colors after you have use +dither -colors 4.

scolby33
Posts: 4
Joined: 2018-11-25T19:24:42-07:00
Authentication code: 1152

Re: Downsizing image with large areas of same color

Post by scolby33 » 2018-11-26T16:47:19-07:00

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

Post Reply