Page 1 of 1

Correct way to lock a palette?

Posted: 2014-02-28T05:15:10-07:00
by trondmm
Hi.

I have a script that converts a 24 bit RGB images to a 16 color palette. There are more than 100000 images at the moment, and it is important that they have the same palette.

I've used Map() to copy the palette from one image to the other, but after we upgraded the production machines to Ubuntu Precise (whick took ImageMagick from 6.0.6.2 to 6.6.9.7), this no longer works. By that I mean that even though the converted image does have the same colors it's supposed to have, the colors in the palette are organised in a different order, which is very unfortunate for us.

Color palette from the palette image:

Code: Select all

  Colormap: 16
         0: (  0,  0,  0) #000000 black
         1: (  0,  0,153) #000099 rgb(0,0,153)
         2: (  0, 25,255) #0019FF rgb(0,25,255)
         3: (  0,153,255) #0099FF rgb(0,153,255)
         4: ( 64,204,255) #40CCFF rgb(64,204,255)
         5: (204,  0,  0) #CC0000 rgb(204,0,0)
         6: (255, 25,  0) #FF1900 rgb(255,25,0)
         7: (255,102,  0) #FF6600 rgb(255,102,0)
         8: (255,179,  0) #FFB300 rgb(255,179,0)
         9: (255,230, 77) #FFE64D rgb(255,230,77)
        10: (255,255, 64) #FFFF40 rgb(255,255,64)
        11: (128,235,255) #80EBFF rgb(128,235,255)
        12: (179,255,255) #B3FFFF rgb(179,255,255)
        13: (255,255,190) #FFFFBE rgb(255,255,190)
        14: (217,255,255) #D9FFFF rgb(217,255,255)
        15: (255,255,255) #FFFFFF white
Colormap from the file imagemagick outputs:

Code: Select all

  Colormap: 16
         0: (255,255,255) #FFFFFF white
         1: (179,255,255) #B3FFFF rgb(179,255,255)
         2: (128,235,255) #80EBFF rgb(128,235,255)
         3: (217,255,255) #D9FFFF rgb(217,255,255)
         4: (  0,  0,  0) #000000 black
         5: (  0,  0,153) #000099 rgb(0,0,153)
         6: (255,255,190) #FFFFBE rgb(255,255,190)
         7: ( 64,204,255) #40CCFF rgb(64,204,255)
         8: (255,255, 64) #FFFF40 rgb(255,255,64)
         9: (255,230, 77) #FFE64D rgb(255,230,77)
        10: (204,  0,  0) #CC0000 rgb(204,0,0)
        11: (255, 25,  0) #FF1900 rgb(255,25,0)
        12: (255,102,  0) #FF6600 rgb(255,102,0)
        13: (255,179,  0) #FFB300 rgb(255,179,0)
        14: (  0,153,255) #0099FF rgb(0,153,255)
        15: (  0, 25,255) #0019FF rgb(0,25,255)
Here's the code I've been using

Code: Select all

my $palette = Image::Magick->new;
$palette->Read("palette.png");
my $img = Image::Magick->new;
$img->Read("24bitrgb.png");
$img->Map(image => $palette, dither => 'False');
$img->Quantize(colors => 16);
$img->Write("16colors.png");
So - I assume this means that I've been using Map() wrong all the time, and it's more or less a coincidense that the palette order was preserved. But, what is the correct way of doing it?

EDIT: Corrected the color palette. The first version I included had fewer colors, because parts of the image was matted out.