Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Average RGB value for a gif image.

Posted on 2004-11-06
4
Medium Priority
?
251 Views
Last Modified: 2008-03-10
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.
0
Comment
Question by:stakor
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 12514847
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
 
LVL 1

Expert Comment

by:jdhoover123
ID: 12520148
Stakor, May I ask what application you have here - why you need this. The answer from ozo looks good.
0
 

Author Comment

by:stakor
ID: 12523592
Sorry, there was a delay in my checking. Thanks for the answer.
0
 

Author Comment

by:stakor
ID: 12524003
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

804 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question