Skip to content

Instantly share code, notes, and snippets.

@JEEN
Created June 11, 2013 10:49
Show Gist options
  • Save JEEN/5756006 to your computer and use it in GitHub Desktop.
Save JEEN/5756006 to your computer and use it in GitHub Desktop.
그림파일의 평균RGB 값 구하기
#!/usr/bin/env perl
use strict;
use warnings;
use GD;
my $img = GD::Image->new($ARGV[0]);
my ($width, $height) = $img->getBounds;
my $n = 0;
my $avg = 0;
for my $y (0 .. $height - 1) {
for my $x (0 .. $width - 1) {
my ($r, $g, $b) = $img->rgb( $img->getPixel($x, $y));
my $rgb = ($r << 16) + ($g << 8) + $b;
$avg = ($n * $avg + $rgb) / ($n + 1);
$n += 1;
}
}
printf "#%06X\n", $avg;
=pod
Usage:
perl average-color.pl [file]
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment