On Tue, 31 Oct 2006 02:38:55 -0400, Gabe Schaffer wrote:
> On 10/30/06, Brian Jackson wrote:

Hi Brian, Gabe

>> Is there a way to get a histogram of an image without writing it
>> to a file using PerlMagick?

Yes. You'll just have to format it for your convenience.

Tested code:
-----><8-----
#!/usr/bin/perl
#
# Note:
#        tab = 4 spaces or die.

use strict;
use warnings;

use Config;
use Image::Magick;

# ----------------

print "Perl V $Config{'version'}. \n";
print "Image::Magick V $Image::Magick::VERSION. \n";

my($old_image)        = Image::Magick -> new();
my($old_name)        = 'pl_ms162.png';

print "Reading old file: $old_name... \n";

my($result) = $old_image -> Read($old_name);
die $result if $result;

print "Read old file: $old_name. \n";
print "Getting width and height... \n";

my(@detail) = $old_image -> Get('width', 'height');

# Warning: Combine next 2 lines into 1.
print "Size: $detail[0] x $detail[1]. Pixel count: @{[$detail[0] * $detail[1]
]}. \n";
print "Getting histogram... \n";

my(@histogram) = $old_image -> Histogram();

print "Elements in histogram array: @{[scalar @histogram]}. \n";

my($total_pixel_count) = 0;

my($red, $green, $blue, $opacity, $count);

while (@histogram)
{
        ($red, $green, $blue, $opacity, $count) = splice(@histogram, 0, 5);

        $total_pixel_count += $count;

        # Warning: Combine next 2 lines into 1.
        print sprintf "Red: 0x%04x. Green: 0x%04x. Blue: 0x%04x. Opacity: 
0x%04x.
Count: %i. \n", $red, $green, $blue, $opacity, $count;
}

print "Total pixel count: $total_pixel_count. \n";
-----><8-----

Output:
-----><8-----
Perl V 5.8.7.
Image::Magick V 6.2.7.
Reading old file: pl_ms162.png...
Read old file: pl_ms162.png.
Getting width and height...
Size: 1024 x 768. Pixel count: 786432.
Getting histogram...
Elements in histogram array: 110.
Red: 0x0000. Green: 0x0000. Blue: 0x0000. Opacity: 0x0000. Count: 5223.
Red: 0x3030. Green: 0x3030. Blue: 0x3030. Opacity: 0x0000. Count: 5918.
Red: 0x0000. Green: 0x4242. Blue: 0x0000. Opacity: 0x0000. Count: 12553.
Red: 0x5959. Green: 0x0d0d. Blue: 0x0000. Opacity: 0x0000. Count: 210.
Red: 0x7070. Green: 0x7070. Blue: 0x7070. Opacity: 0x0000. Count: 1156.
Red: 0x0000. Green: 0x3333. Blue: 0xbfbf. Opacity: 0x0000. Count: 58284.
Red: 0x0000. Green: 0x8080. Blue: 0x0000. Opacity: 0x0000. Count: 9663.
Red: 0x0000. Green: 0x8080. Blue: 0x5555. Opacity: 0x0000. Count: 1943.
Red: 0xb3b3. Green: 0x4040. Blue: 0x0000. Opacity: 0x0000. Count: 1965.
Red: 0xd9d9. Green: 0x5959. Blue: 0x0000. Opacity: 0x0000. Count: 7031.
Red: 0xabab. Green: 0x8080. Blue: 0x4545. Opacity: 0x0000. Count: 2178.
Red: 0x9999. Green: 0xffff. Blue: 0x3333. Opacity: 0x0000. Count: 13425.
Red: 0xdbdb. Green: 0xc4c4. Blue: 0x7575. Opacity: 0x0000. Count: 12727.
Red: 0x8080. Green: 0x8080. Blue: 0x8080. Opacity: 0x0000. Count: 7430.
Red: 0xa1a1. Green: 0xa1a1. Blue: 0xa1a1. Opacity: 0x0000. Count: 231.
Red: 0x8080. Green: 0xc2c2. Blue: 0xffff. Opacity: 0x0000. Count: 90401.
Red: 0x8080. Green: 0xffff. Blue: 0xffff. Opacity: 0x0000. Count: 5153.
Red: 0xeded. Green: 0xd6d6. Blue: 0x8787. Opacity: 0x0000. Count: 15432.
Red: 0xffff. Green: 0xf2f2. Blue: 0xa6a6. Opacity: 0x0000. Count: 13580.
Red: 0xffff. Green: 0xffff. Blue: 0xdede. Opacity: 0x0000. Count: 4991.
Red: 0xe0e0. Green: 0xe0e0. Blue: 0xe0e0. Opacity: 0x0000. Count: 351.
Red: 0xffff. Green: 0xffff. Blue: 0xffff. Opacity: 0x0000. Count: 516587.
Total pixel count: 786432.
-----><8-----
-- 
Cheers
Ron Savage, [EMAIL PROTECTED] on 1/11/2006
http://savage.net.au/index.html
Let the record show: Microsoft is not an Australian company


_______________________________________________
Magick-users mailing list
[email protected]
http://studio.imagemagick.org/mailman/listinfo/magick-users

Reply via email to