Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Need module for retrieving pixels from GIF format image...

by TedPride (Priest)
on Mar 22, 2006 at 07:21 UTC ( [id://538436]=perlquestion: print w/replies, xml ) Need Help??

TedPride has asked for the wisdom of the Perl Monks concerning the following question:

I Googled and Super Searched for this, and while I found lots of information on generating GIFs, and even on GIF format, I couldn't find anything for retrieving pixel values. Ideally, what I want is something that will load a GIF and return dimensions, a list of colors (in my case, two colors), and the pixel values for the image. Short of that, an explanation of how to decode GIF pixel values would be helpful, since they seem to be compressed and I can't figure out how to decode the compression. Retrieving color info and dimensions was a lot easier.
  • Comment on Need module for retrieving pixels from GIF format image...

Replies are listed 'Best First'.
Re: Need module for retrieving pixels from GIF format image...
by timos (Beadle) on Mar 22, 2006 at 08:23 UTC
    use GD; $image = GD::Image->new($filename); $index = $image->getPixel(20,100); ($r,$g,$b) = $myImage->rgb($index);
Re: Need module for retrieving pixels from GIF format image...
by Corion (Patriarch) on Mar 22, 2006 at 07:25 UTC

    There are many image processing modules - the three major ones are Image::Magick, Imager and GD - any one of the three should do what you want. None of the three are convenient to build, as all three rely on lots and lots of external libraries to do the heavy lifting.

      You can definitely do it with ImageMagick/PerlMagick, I've done it myself ages ago -- with a function called getPixel(x,y) which returns an RGB value, if I remember correctly.


      ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
      =~y~b-v~a-z~s; print

      I have used GD quite extensively on solutions for the http://www.pythonchallenge.com/ puzzles. It allows for you to getPixel($x,$y) and setPixel($x,$y,$color) if needed. The only drawback is that I have even had difficulty installing this module from the PPM on win32. I never seems to have the same issue twice.

      Thank you,
      Greg W
Re: Need module for retrieving pixels from GIF format image...
by zentara (Archbishop) on Mar 22, 2006 at 13:06 UTC
    This script ( from author unknown) works on a gif I tested.
    #!/usr/bin/perl #None of these is fast, mainly because of the overhead of calling Perl #code for each pixel. use strict; my $imagefile = shift or die "No file specified\n"; sub rgba2hex { sprintf "%02x%02x%02x%02x", map { $_ || 0 } @_; } { use Imager; my %colors; my $img = Imager->new(); $img->open( file => $imagefile ) or die $img->errstr; my ( $w, $h ) = ( $img->getwidth, $img->getheight ); for my $i ( 0 .. $w - 1 ) { for my $j ( 0 .. $h - 1 ) { my $color = $img->getpixel( x => $i, y => $j ); my $hcolor = rgba2hex $color->rgba(); print "$hcolor "; $colors{$hcolor}++; } } printf "Imager: Number of colours: %d\n", scalar keys %colors; } { use GD; my %colors; my $gd = GD::Image->new($imagefile) or die "GD::Image->new($imagef +ile)"; my ( $w, $h ) = $gd->getBounds(); for my $i ( 0 .. $w - 1 ) { for my $j ( 0 .. $h - 1 ) { my $index = $gd->getPixel( $i, $j ); my $hcolor = rgba2hex( $gd->rgb($index), 0 ); $colors{$hcolor}++; } } printf "GD: Number of colours: %d\n", scalar keys %colors; } { use Image::Magick; my %colors; my $img = Image::Magick->new(); my $rc = $img->Read($imagefile); die $rc if $rc; my ( $w, $h ) = $img->Get( 'width', 'height' ); for my $i ( 0 .. $w - 1 ) { for my $j ( 0 .. $h - 1 ) { my $color = $img->Get("pixel[$i,$j]"); my $hcolor = rgba2hex split /,/, $color; $colors{$hcolor}++; } } printf "Image::Magick: Number of colours: %d\n", scalar keys %colo +rs; }

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://538436]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-18 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found