Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert image to indexed color with custom palette through console

I have image.png in truecolor, palette.png (N colors, where N>256) or text file, where list RGB color palette. How to get a picture with this palette?

If I use imagemagick:

convert image.png -remap palette.png remap_image.png

It does not work.

convert image.png -map palette.png remap_image.png

Gives a very bad quality. The image is very noisy. File size is bigger than before.

GIMP gives best quality:

Сonvert image to indexed color > use custom palette

But GIMP is GUI. I need to convert a lot images in the console without running the gimp and X.org.

like image 559
Smirnov Avatar asked Sep 30 '11 10:09

Smirnov


People also ask

How do I create an index color in Photoshop?

To convert to indexed color, you must start with either a grayscale or an RGB image. Choose Image > Mode > Indexed Color. Click OK to flatten layers.

What is an index color palette?

“Indexed color” means that the set of colors in the image, its palette, is stored in a color table. Each pixel in the image contains a reference (or “index”) to a table cell containing the color for that pixel. In Photoshop, you can view the table for an indexed color image by selecting Image → Mode → Color Table.


1 Answers

Using a shared palette across multiple images requires a carefully crafted palette. If you don't take great care when using the palette of a single image across many images, the result will be poor.

This needn't be complicated though. If you have accesss to the GIMP (or other tool) which supports truecolor graphics, you can create a large image and fit all of the smaller images into it, then quantize the image to N colors, then use that palette as the source.

you should be able to closely mimic GIMP's behavior in the console using ImageMagick

Once you've got a truecolor image with all the colors you want to quantize,

# Create an 8-bit png from our source, with a 235-color palette as an example.
convert truecolor_source.png -colors 235 palette.png

# Create an 8-bit png from an arbitrary image and use the palette in palette.png
convert sample.png -map palette.png output.png

There are a number of options for down-sampling colors, like dithering. See the ImageMagickv6 example page for an excellent overview with example pictures and code.

Although I still don't exactly understand what you want to do, your currently most recent comment ("Yes, from RGB to palette will set independently. Need set correct quantity of colors"), it sounds like all you want to do is set a strict limit on the amount of colors of a bunch of images, but they don't need to use the same palette.

In that case, the solution is very simple:

convert sample.png -colors 135 output.png

Try playing with the quantization options if the result isn't to your satisfaction.

If the output image is too large for your liking, you can experiment with the -quality option.

If this still isn't satisfactory, please try to explain your goal in a more detailed manner. Good luck!

like image 62
gamen Avatar answered Sep 30 '22 09:09

gamen