in reply to Reading an image palette

I knew that IM could do it... it's just such a big program things are hard to find and make work. Anyways, this gives output similar to BrowserUk's GD script.

This is only quickly tested, I may have the shift order wrong, etc. The color count dosn't seem to agree with GD. Please test yourself to verify worthiness.

#!/usr/bin/perl use warnings; use strict; use Image::Magick; my $file = shift or die "Need a file $!\n"; my $img = Image::Magick->new; $img->ReadImage($file); $img->Quantize( colors => 18 ); $img->Set( type => 'Palette' ); #$img->Write("$0.png"); # if you want a pixel by pixel list of every color # a huge slow output #$img->Set(magick => 'txt'); #$img->Write("$0.txt"); #histogram #returned values are an array of #red, green, blue, opacity, and count values. my $tot = 0; my (@colors) = $img->Histogram(); #print join "\n\n", @colors; # probably some array slice would be better than multi-shifts, # any code improvement's welcome while (1){ if (scalar @colors == 0){last} my $r = shift @colors; my $g = shift @colors; my $b = shift @colors; my $o = shift @colors; my $count = shift @colors; $tot++; print "$count ($r,$g,$b) at $o opacity\n"; } print "\n$tot total colors\n";

I'm not really a human, but I play one on earth Remember How Lucky You Are

Replies are listed 'Best First'.
Re^2: Reading an image palette
by zentara (Cardinal) on Oct 06, 2008 at 13:27 UTC
    As a followup as to why there is sometimes a discrepancy between the GD and IM output, it's because GD gets the actual palette, whearas the IM histogram method only gets the actual colors used. So you may have a palette with 17 colors, but only 15 may be used. I've also seen palettes with black(0,0,0) listed multiple times....that will screwup accurate color counts.

    I'm not really a human, but I play one on earth Remember How Lucky You Are