jm666 jm666 - 6 months ago 29
Perl Question

How to get the Image::Magick's blob (raw) image from the Imager?

How to get the same

raw data
from the Imager as from the Image::Magick's
ImageToBlob
function for the
GRAY/8bit
?

#Image::Magick
my $raw_magic = $im->ImageToBlob(magick => 'GRAY', depth => 8);

#Imager??? the simple 'gray' preset gives different data
my $gray = $img->convert(preset => 'gray');
$gray->write(data => \my $raw_imager, type => 'raw');


Probably the
convert
using the
matrix => ...
could help, but can't figure how to use it..

(I need pass the raw data to some another module, which works OK with the
$raw_magic
- so, looking for how to get the same data from the
Imager
.)

If someone want play, here is my test image
qrcode.png

enter image description here
and also my test script.

use 5.014;
use warnings;
use Image::Magick;
use Imager;

my $file = shift // 'qrcode.png';
die "missing $file" unless -f $file;

#Image::Magick
my $im = Image::Magick->new;
$im->Read($file);
my $raw_magic = $im->ImageToBlob(magick => 'GRAY', depth => 8);
#hexdump($raw_magic);

#Imager
my $img = Imager->new;
$img->read(file=>$file, type=>'png') or die 'read:', $img->errstr;
my $gray = $img->convert(preset => 'gray');
$gray->write(data => \my $raw_imager, type => 'raw') or die 'write:', $gray->errstr;
#hexdump($raw_imager);

say "Different" if $raw_magic cmp $raw_imager;

sub hexdump {
my $data = shift;
my $n;
print $_, (++$n % 16) ? " " : "\n"
for unpack '(A2)*', unpack 'H*', $data;
print "\n";
}


EDIT



Adding some background information. I want to use the Barcode::ZBar package. So, calling my
decode_qr
with the raw data produced by the Image::Magick, the QR-decode decodes correctly the "hello", using the Imager's data doesn't.

decode_qr($raw_magic, $im->Get(qw(columns rows)), 'magick');
decode_qr($raw_imager, $gray->getwidth(), $gray->getheight(), 'imager');

sub decode_qr {
my($raw, $w, $h, $from) = @_;

path($from . '.raw')->spew_raw($raw); #save the raw data

my $zimage = Barcode::ZBar::Image->new;
$zimage->set_format('Y800');
$zimage->set_size( $w, $h );
$zimage->set_data($raw);
Barcode::ZBar::ImageScanner->new->scan_image($zimage);
for my $sym ($zimage->get_symbols) {
say join(':', $from, $sym->get_type(), $sym->get_data());
}
}

Answer

I have had a little time to test this out and there is something odd going on. I created a dummy qrcode.pnm with ImageMagick:

convert -size 1x1! -depth 8 -compress none xc:black xc:white xc:gray xc:gray30 xc:gray90 +append qrcode.pnm

And with that, the following code works as expected:

#!/usr/bin/perl
use 5.014;
use warnings;
use Image::Magick;
use Imager;

my $file = shift // 'qrcode.pnm';
die "missing $file" unless -f $file;

#Image::Magick
my $im = Image::Magick->new;
$im->Read($file);
my $raw_magic = $im->ImageToBlob(magick => 'GRAY', depth => 8);
hexdump($raw_magic);

#Imager
my $img = Imager->new;
$img->read(file=>$file,type=>'pnm') or die 'read:', $img->errstr;
my $gray = $img->convert(matrix => [[1,0,0]]);
$gray->write(data => \my $raw_imager, type => 'raw') or die 'write:', $gray->errstr;
hexdump($raw_imager);

say "Different" if $raw_magic cmp $raw_imager;

sub hexdump {
    my $data = shift;
    my $n;
    print $_, (++$n % 16) ? " " : "\n"
        for unpack '(A2)*', unpack 'H*', $data;
    print "\n";
}

Output

00 ff 7e 4d e5
00 ff 7e 4d e5

Not sure what that proves, or if it is useful but will spend some more time tomorrow.

Comments