Thank you, this is wonderful!

I'm using it as:

sub palette_rgb_gd { my $abs_path = _abs_path($_[0]) or return; my @colors; require GD; my $img = GD::Image->new( $abs_path ) or die $!; my $colors = $img->colorsTotal or carp("$abs_path is a truecolor image (no palette)?"); for ( 0 .. $colors ) { my $rgb = sprintf '%02x%02x%02x', $img->rgb( $_ ); #debug( $rgb); #printf "%5d: R:%2x G:%2x B:%2x\n", $_, $img->rgb( $_ ); push @colors , $rgb; } return @colors; }
This is way better than what I was doing before!
sub identify_im { my $abs_path = _abs_path($_[0]) or return; my %HASH; my $cmd = "identify -verbose '$abs_path'"; my @_i = split(/\n/, `$cmd`); if( $? ){ carp("problem with : $cmd, $abs_path, $?") and return; } @_i and scalar @_i or carp("no output with : $cmd, $abs_path") and +return; my $lastkey; for my $_line (@_i){ $_line=~s/^\s+|\s+$//g; $_line=~s/([^\:]+)\://; my($k,$v) = ($1,$_line); if ($k=~/^Histogram$|^Colormap$/){ $HASH{$k}={}; $lastkey =$k; } elsif($lastkey=~/^Histogram$|^Colormap$/ and $k=~/^\d+$/ ){ $HASH{$lastkey}->{$k} = $v; } else { $HASH{$k}= $v; $lastkey = $k; } } return \%HASH; } sub palette_rgb_im { my $abs_path = _abs_path($_[0]) or return; my $h = identify_im($abs_path) or return; my $colors = $h->{Colormap} || $h->{Histogram} ; $colors or carp("No Colormap or Historgram in $abs_path") and retur +n; my @colors; for ( keys %$colors ){ my $_line = $colors->{$_}; $_line=~/\(\s*([0-9]+),\s*([0-9]+),\s*([0-9]+)\)/ or die("cant m +atch into $_line"); my ($r,$g,$b) = ($1,$2,$3); debug( "$r $g $b"); #push @colors, "$r$g$b"; my $rgb = sprintf '%2x%2x%2x', $r, $g , $b; push @colors, $rgb; } return @colors; }

In reply to Re^2: Reading an image palette by leocharre
in thread Reading an image palette by leocharre

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.