Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

ImageMagick - why do these snippets act differently?

by pete_l (Novice)
on Aug 21, 2014 at 13:30 UTC ( #1098232=perlquestion: print w/replies, xml ) Need Help??

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

There is a small piece of code that does what I expect it to:
#!/usr/bin/perl use Image::Magick; $ipimage = Image::Magick->new(size=>'640x480'); $ipimage->Read("logo:"); $blob1=$ipimage->ImageToBlob(); $opimage = Image::Magick->new(size=>'640x480'); $opimage->BlobToImage($blob1); $opimage->Write("t1.jpg");
It writes a JPEG of the ImageMagick logo. Here is a second piece of code. The only difference is that I have specified an RGB format for the image:
#!/usr/bin/perl use Image::Magick; $ipimage = Image::Magick->new(size=>'640x480'); $ipimage->Set(magick=>'RGB'); $ipimage->Read("logo:"); $blob1=$ipimage->ImageToBlob(); $opimage = Image::Magick->new(size=>'640x480'); $opimage->Set(magick=>'RGB'); $opimage->BlobToImage($blob1); $opimage->Write("t2.jpg");
In this case, the output is still a 640x480 JPEG image. However, the icon is a quarter the size, it's repeated in the top left and right quadrants (the lower half of the image being black) and the image colours are wrong.

I have looked for any worked examples of BlobToImage and come up with nothing. What documentation I have found seems to be abstract API definitions, but without enough information to say for sure if I'm calling it correctly. I'd really appreciate someone explaining to me why I get these different outputs, and how to call BlobToImage for an RGB image (since I need to manipulate the individual colour pixels and GetPixel/SetPixel is far too slow) to create the sort of output that the first code example gives.

With thanks

Replies are listed 'Best First'.
Re: ImageMagick - why do these snippets act differently?
by tangent (Vicar) on Aug 21, 2014 at 15:42 UTC
    Try setting the depth of the new image to 8 (it defaults to 16)
    $opimage->Set(magick=>'RGB', depth=>8);
    You can get the depth of the original image like this:
    my $depth = $ipimage->Get('depth');
      Try setting the depth of the new image to 8 (it defaults to 16)

      Thank you, that cracked it!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2021-04-16 21:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?