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

Hello

I have grabbed a jpg image from an IP camera with Image::Grab. So I have a valid image object.

I want to pass the image to a subroutine. Within the subroutine, I want to use an Image::Magick method to do a read test on the image. </p)

Back in the main code, I want to use the grabbed image object and annotate/composite using Image::Magick methods.

So how do I pass a Image::Grab object to a sub-routine, then apply a read-only Image::Magick method to it? How do I take a Image::Grab object and apply a write Image::Magick method to it?

The questions are similar but different. At present, I am achieving this by saving the Image::Grab jpg to hard drive, then reading it into a Image::Magick object. It is slow and inefficient.

Dazz

  • Comment on Howto convert Image::Grab jpg object to Image::Magick object???

Replies are listed 'Best First'.
Re: Howto convert Image::Grab jpg object to Image::Magick object???
by haukex (Archbishop) on Mar 27, 2017 at 08:36 UTC

    If I understand the Image::Grab docs correctly, it just returns the image as binary data. According to the docs, Image::Magick supports reading filehandles, which you could use with an in-memory filehandle (open), as well as reading data in memory directly:

    my $image = Image::Magick->new(magick=>'jpg'); open my $fh, '<', \$imgdata or die $!; $image->Read(file=>$fh); # - or - $image->BlobToImage($imgdata);

    (untested)

Re: Howto convert Image::Grab jpg object to Image::Magick object???
by dazz (Beadle) on Mar 27, 2017 at 08:51 UTC
    Hi Do I need to use Blobs?? I have just discovered them.
      Do I need to use Blobs??

      Did you try the code I posted? As far as I can tell from the docs, this should be all you need to get the $imgdata I showed in my other post:

      use Image::Grab qw/grab/; my $imgdata = grab('http://upload.wikimedia.org/wikipedia/commons/1/1a +/Image_upload_test.jpg'); # - or - my $pic = Image::Grab->new; $pic->url('http://upload.wikimedia.org/wikipedia/commons/1/1a/Image_up +load_test.jpg') $pic->grab; my $imgdata = $pic->image;

      (untested)

        Hi

        I found this code sample that converts from GD to Image::Magick objects. It is like I want to do and it should work between any objects.

        Haukex, I will give your code a go.

        #!/user/bin/perl use strict; use Image::Magick; use GD; # First read an image with Image::Magick my $im = new Image::Magick; my $status = $im->Read('monkey.png'); if ($status) { die "$Status!"; } # Convert to a blob and hand off to GD my @blobs = $im->ImageToBlob(); my $gd = GD::Image->newFromPngData($blobs[0]); my $red = $gd->colorAllocate(255,0,0); # Outline in Red $dg->rectangle(0,0, (map {$_-1} $gd-?getBounds), $red); # There and back again my $im2 = new Image::Magick; $im2->BlobToImage($gd->png()); $im2->Write('png:monkeyout.png');