Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

reading in raw data into perl's ImageMagick

by bet_shemesh_debugger (Novice)
on Aug 19, 2010 at 10:56 UTC ( #855996=perlquestion: print w/ replies, xml ) Need Help??
bet_shemesh_debugger has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl variable $data which contains the actual raw bytes of an image. I do not want to have to save this to a file just so I can read it into an ImageMagick object to change it. Is there any way to read this raw data into the object (and extract it out as raw data)?

$image->ReadImage(file=>\$data);
$image->Read($data);
This do not work (along with variations thereof)

Comment on reading in raw data into perl's ImageMagick
Re: reading in raw data into perl's ImageMagick
by BrowserUk (Pope) on Aug 19, 2010 at 11:18 UTC

    Open the scalar containing the data as a ramfile and then supply the glob to Read:

    open RAM, '<:raw', \$data; $image->Read( file => \*RAM ); close RAM;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Still didn't work but thanks for trying.
Re: reading in raw data into perl's ImageMagick
by apl (Monsignor) on Aug 19, 2010 at 12:08 UTC
    Have you tested for successful completion of open, read, etc.? If not, do so. If yes, are you printing out the returned error-code?
Re: reading in raw data into perl's ImageMagick
by zentara (Archbishop) on Aug 19, 2010 at 12:33 UTC
    ImageMagick uses "blobs" for converting to and from raw image data in scalars. Here are a few simple usages:

    #!/usr/bin/perl use warnings; use strict; use Image::Magick; my $imgfile = shift; my $image = Image::Magick->new(); $image->Read($imgfile); my $blob = $image->ImageToBlob(); #raw image data is now in scalar $blob #and the opposite my $output = Image::Magick->new(magick=>'jpg'); $output->BlobToImage( $blob ); $output->Resize(geometry=>'160x120'); $output->Write('z.jpg');
    another example to show the scalar blob does indeed contain image data

    #!/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.
    Old Perl Programmer Haiku
      The blob thing worked great. It helped for both the input and output!!!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-12-25 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls