Average RGB value for a gif image.

I am trying to use perl to find out what the average rgb value is for an image. I would like to be able to get a hex value like FF4422 out as the result if possible. I also imagine that I will have to use image::magick. But I am all ears to any way of getting this done. I have found snippets about getting a alue for a pixel here, but not an image.

So, anyone know how to get an average rgb value out of a .gif file using perl?

Thanks.
stakorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
my $input = new Image::Magick;
$input->Read ('image.gif');
($h,$w)=$input->get('height','width');
for $x (0..$h-1){
    for $y (0..$w-1){
      ($r,$g,$b) = split/,/, $input->get("pixex($x,$y)");
      $sumr += $r;
      $sumg += $g;
      $sumb += $b;  
   }
}
$avgr = $sumr/($h*$w);
$avgg= $sumg/($h*$w);
$avgb = $sumb/($h*$w);

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdhoover123Commented:
Stakor, May I ask what application you have here - why you need this. The answer from ozo looks good.
0
stakorAuthor Commented:
Sorry, there was a delay in my checking. Thanks for the answer.
0
stakorAuthor Commented:
The code I am using is:

#!/usr/bin/perl

use Image::Magick;

my $input = new Image::Magick;
$input->Read ('image.gif');
($h,$w)=$input->get('height','width');
for $x (0..$h-1){
    for $y (0..$w-1){
      ($r,$g,$b) = split/,/, $input->get("pixel($x,$y)");
      $sumr += $r;
      $sumg += $g;
      $sumb += $b;
   }
}
$avgr = $sumr/($h*$w);
$avgg= $sumg/($h*$w);
$avgb = $sumb/($h*$w);

print "Red: $sumr\n";
print "Green: $sumg\n";
print "Blue: $sumb\n";
print "\n\n";
print "Red (A): $avgr\n";
print "Green (A): $avgg\n";
print "Blue (A): $avgb\n";
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.