JoBe
JoBe

Reputation: 407

Convert jpg to png with predefined color codes?

I've got some maps, based on 66 colors, that I unfortunately saved in jpg for a couple of months ago.

I've discovered that I can convert to png, and set it to be 66 colors, BUT, is there anyway to predefine those 66 colors, so for example the colors close to #888888 will be converted to #888888 and not to 878787 and 878887.

One issue is also that the maps don't contain all 66 colors,(usually around 30) so now I might get 4 different type of white. (that's why I need this solution)

is this even possible with imagemagick?

Upvotes: 2

Views: 129

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207345

You can make a 66x1 pixel "swatch" of the 66 colours you want by taking one of your original PNG images and extracting the unique colours like this:

magick original.png -unique-colors swatch.png

Then you can apply it like this:

magick unhappy.jpg -remap swatch.png result.png

More details here.


Here's a concrete example. Here's our map:

enter image description here

First, make a swatch of the 5 colours we want to appear in our output image:

magick xc:"rgb(10,100,140)" xc:"rgb(240,190,120)" xc:"rgb(70,130,30)" xc:"rgb(220,230,230)" xc:"rgb(40,80,50)" +append swatch.png

enter image description here

Now remap all the colours in the original to the 5 colours in the swatch:

magick map.jpg +dither -remap swatch.png result.png

enter image description here


Alternatively, we could let ImageMagick choose the best colours for the swatch like this:

magick map.jpg -colors 7 -unique-colors swatch.png

enter image description here

And remap just the same as before but using the colours ImageMagick chose:

magick map.jpg +dither -remap swatch.png result.png

enter image description here


Note that you can use hex codes (or HSL, or Lab colours) just the same:

convert xc:"#0a658c" xc:"#f0be78" xc:"#46821e" xc:"#dce6e6" xc:"#285032" +append swatch.png

Note that the above commands assume ImageMagick v7. If you are obliged to use old v6 syntax, replace magick with convert.

Upvotes: 2

Related Questions