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

Having written img2txt which works well with RAW images, I want to be able to use more standard graphics formats: GIF, PNG, JPEG, BMP...

I had a look at gd.pm, but it doesn't appear to allow pixel-by-pixel reading, which is critical to my plans. Any suggestions?

Replies are listed 'Best First'.
Re: Pixel by pixel reading of images
by merlyn (Sage) on Feb 24, 2001 at 21:24 UTC
      So Image::Magick can read the FIASCO format?
        Apparently not (at least, not yet). As you know, that format is brand-spanking new off some guy's doctoral thesis. I don't imagine that it's well-known but it certainly caught my eye. List of Image::Magick supported formats // The link bastard provides is good for reading up on this potentially powerful (and, if nothing else, interesting) image format based on "fractal magic".
        AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Pixel by pixel reading of images
by Chmrr (Vicar) on Feb 25, 2001 at 03:51 UTC

    You might want to look a little closer. GD::getPixel does what you want to do, I think. Combine it with GD::rgb to get the rgb color at that point, like so:

    my $index = $myImage->getPixel(20,100); my ($r,$g,$b) = $myImage->rgb($index);

    Hope this helps.


    perl -e 'print "I love $^X$\"$]!$/"#$&V"+@( NO CARRIER'
Re: Pixel by pixel reading of images
by dmckee (Scribe) on Feb 25, 2001 at 18:19 UTC
    Thanks for your help - I'm now using Image::Magick. However, it was hard to come by the docs, so just to help anyone else passing this way...
    use Image::Magick; my $IMimage = Image::Magick->new; $IMimage->Read($filename); $readval=$IMimage->Get('pixel['.$xcount.','.$ycount.']');
    It doesn't appear to be documented on the Image Magick perl page... it returns "red,green,blue,alpha" (which might interface with a four-element array). All of the colours are 0..255

      Hello,
      thank you for posting that - its certainly saved me - I've been running all over looking for something just like that.

      My problem happens to be that I need the graylevels, not the RGBA values.

      Would you, or anyone else, happen to know exactly how to retrieve that value for each pixel in my .pgm image using Image:Magick? I'm using the code posted above to get the RGB values, but I can't do anything with them really... Pointers to more Image::Magick documentations are welcome as well.

      Thanks,

      ~mina

        As merlyn alluded to above, you need to add an RGB to luminance conversion. There is no one right way to do this, but the one used in American TV broadcasting is:

           Luma (gray value) = 0.299*Red + 0.587*Green + 0.114*Blue

        Reference: Keith Jack, Video Demystified, 2nd Ed. (HighText Publications)

Re: Pixel by pixel reading of images
by bastard (Hermit) on Feb 25, 2001 at 12:01 UTC
    Chmrr is correct. To get html usable color codes out add:
    my $color = sprintf "%02x%02x%02x", $r, $g, $b;
    This will create the 3 sets of 2 digit hex numbers you are looking for.

    I did an image to table converter here (colored cells)
    I believe there are a few other progs like this here, try the code vault.

    Snowcrash seems to have built(or used) something similar to what you are doing, but i'm not sure where his code is.

Re: Pixel by pixel reading of images
by fundflow (Chaplain) on Feb 25, 2001 at 00:42 UTC
    PPM is a standard format that is very easy to read and write. Many programs support it (e.g. ImageMagic).

    If this is enough, I/O routines for it are available at Japhoto.pl!.

    Cheers