views:

403

answers:

2

I have an image I have manipulated with GD::Image and I want to do further manipulations with Image::Magick. I'd like to avoid writing the image out to disk just so Image::Magick can read it in. Image::Magick's Read function will accept a filehandle as a parameter, so I'm trying to pass it an IO::Scalar object I created with the output from GD::Image.

However, since an IO::Scalar object can be treated as a string, it looks like Image::Magick is interpreting the contents of the image as a filename which it cannot find, and Read() fails.

Is there another way to create a filehandle from a scalar that behaves more like a regular filehandle, or is there another simpler way to accomplish what I'm trying to do here?

my $FH = new IO::Scalar \$image_bin;
my $magick = Image::Magick->new;
my $response = $magick->Read(file => $FH);

$response is:

"Exception 435: unable to open image `????': No such file or directory"
A: 

From the docs...

To read an image in the GIF format from a Perl filehandle, use:

$image = Image::Magick->new;
open(IMAGE, 'image.gif');
$image->Read(file=>\*IMAGE);
close(IMAGE);

So... I think a reference to your filehandle (\$FH) in your example, instead of just the filehandle, should do the trick?

Edit: To respond to brian d foy, this is what I was suggesting trying:

my $image = Image::Magick->new;
open my $fh, 'image.gif';
binmode $fh;
$image->Read( file => \$fh );
close $fh;

On my system, at least, this seg faults.

I'll let this post stand as an example of what doesn't work. :P

oeuftete
Using bareword filehandle and references to typeglobs is pretty ancient, which is why he's trying to use a scalar reference instead. Apparently PerlMagick doesn't understand this yet.
brian d foy
Fair enough, although I just posted the doc snippet verbatim and suggested trying with a real $fh reference.But... that way seg faults for me when I do try it.
oeuftete
+6  A: 

I think you are looking for BlobToImage:

#!/usr/bin/perl

use strict;
use warnings;

use File::Slurp;
use Image::Magick;

my $image_bin = read_file 'test.jpg', binmode => ':raw';

my $magick = Image::Magick->new;

$magick->BlobToImage( $image_bin );

$magick->Resize( geometry => '64x64' );

$magick->Write( 'test-out.jpg' );

__END__
Sinan Ünür
I saw that function, but for some reason didn't think it was what I was looking for. (It sounded to me like it was supposed to accept a perl array of rgb values or something.) Thanks a bunch, that did it.
Ryan Olson
Glad it helped. Good luck.
Sinan Ünür