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

My image is inside $image.
$image->Write("gif:-");
I need to use gzip encoding. The encoding part works for me, but I don't understand how to get the raw image data from PerlMagick to encode it. If I run:
print $image;
I receive
Image::Magick=ARRAY(0x847d3e0)
Is there a way to extract this data instead of letting PerlMagick write it?

Replies are listed 'Best First'.
Re: Gzip content-encoding and PerlMagick
by zentara (Cardinal) on Aug 13, 2008 at 16:39 UTC
    You want to convert your $image to an inline scalar varaible, which IM calls a "blob". Something like this should do it.
    my $blob = $image->ImageToBlob(); #now your image is in the scalar $blob and you can gzip it #and write it out to file any way you can get to work

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      The thing is, I think ImageToBlob() returns raw image data. As the file type is not even defined yet. It is normally defined on writing the image data to STDOUT:
      binmode STDOUT; $image->Write("gif:-");
      Could I write to a container like <CONTAINER> and from there print it out to STDOUT? I am asking this because I need to apply Gzip to the output to speed up the transfer of the image to the browser, therefore I need to have the image data in a variable.
        It's not documented very well, but you can set the file type before you create the blob, with the "magick" option.
        $image->Set(magick=>'gif'); #set it to any type that's IM legal my $blob = $image->ImageToBlob();
        Here is a working proof.
        #!/usr/bin/perl use warnings; use strict; use Image::Magick; my $image = Image::Magick->new( size => "600x600" ); $image->Read("xc:white"); $image->Draw( primitive => 'line', points => "300,100 300,500", stroke => '#600', ); $image->Set(magick=>'gif'); my $blob = $image->ImageToBlob(); open(FH,"> $0.gif")or die "$!\n"; print FH $blob; close FH;

        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Gzip content-encoding and PerlMagick
by Burak (Chaplain) on Aug 13, 2008 at 18:16 UTC
    Not sure why you need gzip, it is possible to use ImageMagick's own compression methods (None, BZip, Fax, Group4, JPEG, JPEG2000, LosslessJPEG, LZW, RLE, Zip) I think. Code below is (not exactly) what I use in GD::SecurityImage:
    $image->Set( magick => 'png' ); $image->Set( compression => 'Zip' ); print $image->ImageToBlob;
      Thanks, you're completely right. Much easier to let PerlMagick do it self. I know this is subjective, but what's the best compression to use for JPEG images, esp map images? I already reduced the amount of colours to 256. Is JPEG2000 > JPEG? BZip, Fax, Group4, JPEG, JPEG2000, LosslessJPEG, LZW, RLE, Zip I know for GIF only LZW can be used, correct?
        From Wikipedia's entry for JPEG 2000:
        As of 2008, JPEG 2000 is not widely supported in web browsers...
        JPEG is designed for photographs, so it's appropriate to use if your map has many color gradations, such as a relief map.

        If you have a street map with lines and regions of constant solid color, GIF or PNG is likely better due to the lossless compression. Over-compressed JPEGs of this type of image look poor due to the artifacts from the lossy compression.

Re: Gzip content-encoding and PerlMagick
by dwm042 (Priest) on Aug 13, 2008 at 16:08 UTC
    cowgirl, how did you create $image? Knowing what it is might help us help you get the data you want.

    David.