[magick-users] histogram and PerlMagick
Ron Savage
ron at savage.net.au
Tue Nov 7 03:11:22 CST 2006
On Wed, 1 Nov 2006 19:42:05 -0700, Brian Jackson wrote:
Hi Brian
> convert image.jpg -channel red histogram:-|convert - red.png
Here's a program which outputs the same data as above. The vertical scale is
different, though.
-----><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) = 'ag000602.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');
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($max_pixel_count) = 0;
my($total_pixel_count) = 0;
my(@color) = qw/red green blue/;
my($bit_count) = 9;
my($power_of_2) = 2 ** $bit_count;
print "Bit count: $bit_count. Power of 2: $power_of_2. \n";
my(%color, $count, %count);
my($opacity);
my($rgb);
$count{$_} = [(0) x 256] for (@color);
while (@histogram)
{
($color{'red'}, $color{'green'}, $color{'blue'}, $opacity, $count) =
splice(@histogram, 0, 5);
$count{$_}[$color{$_} >> $bit_count] += $count for (@color);
$max_pixel_count = $count if ($count > $max_pixel_count);
$total_pixel_count += $count;
#print sprintf "Red: 0x%04x. Green: 0x%04x. Blue: 0x%04x. Opacity: 0x%04x.
Count: %10i. \n", $color{'red'}, $color{'green'}, $color{'blue'}, $opacity,
$count;
}
print "Max pixel count: $max_pixel_count. \n";
print "Total pixel count: $total_pixel_count. \n";
my($x);
=pod
print "Counts: \n";
print 'index', join(' ', map{sprintf '%8s', $_} @color), "\n";
for $x (0 .. 255)
{
print sprintf '%5i', $x;
print join(' ', map{sprintf '%8i', int($count{$_}[$x] / $power_of_2)} @color);
print "\n";
}
=cut
print "Creating images of size: 256 x $power_of_2. \n";
my($image);
for $rgb (@color)
{
$image = Image::Magick -> new(size => "256x$power_of_2");
$result = $image -> Read('xc:black');
die $result if $result;
for $x (0 .. 255)
{
$count = int($count{$rgb}[$x] / $power_of_2);
$result = $image -> Set("pixel[$x, @{[$power_of_2 - $_]}]" => $rgb) for (0 ..
$count);
die $result if $result;
}
$result = $image -> Write("$rgb.png");
die $result if $result;
print "Wrote $rgb.png. \n";
}
-----><8-----
--
Cheers
Ron Savage, ron at savage.net.au on 7/11/2006
http://savage.net.au/index.html
Let the record show: Microsoft is not an Australian company
More information about the Magick-users
mailing list