[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to count % color coverage?



* Paolo Pedaletti <paolo.pedaletti@unimib.it> [2005-08-26 11:37]:

> may be someone here knows if exist a tool (imagemagick?) that can count
> how mutch black or white exist in a photo/image The problem is: I have
> a photo with spots with different size. I want to know how much this
> spost fill the photo surface (in %) and I want to do it via script (no
> human interaction, if possible) Do you know something usefull?

Try the simple-minded Perl script attached below.  Run it like this (you
will need the libgd-gd2-perl package):

$ ./color-count.pl test.png
[255,  0,  0]:   458
[129, 92, 35]:   140
[255,255,255]:  1467
[129,188,131]:   435

The output format is [red,blue,green]: pixel-count.

Only works with PNG files, but you can transform your GIF file using the
convert command from ImageMagick.

Hope this helps.

-- 
Rafael
#!/usr/bin/perl -w

use GD;

die "Usage: $0 file.png\n" if $#ARGV != 0;
die "Cannot open file $ARGV[0]" if not -r $ARGV[0];

$image = GD::Image->newFromPng($ARGV[0], 1);

($width, $height) = $image->getBounds();
%count = ();

for $i (0 .. $width - 1) {
  for $j (0 .. $height - 1) {
    $count {$image->getPixel ($i, $j)} ++;
  }
}

for $k (keys %count) {
  ($red, $green, $blue) = $image->rgb ($k);
  printf ("[%3d,%3d,%3d]: %5d\n", $red, $green, $blue, $count {$k});
}

Attachment: test.png
Description: PNG image


Reply to: