如何从热像仪中获取Image :: Magick的Blob(原始)图像? [英] How to get the Image::Magick's blob (raw) image from the Imager?

查看:68
本文介绍了如何从热像仪中获取Image :: Magick的Blob(原始)图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从还有我的测试脚本.

 使用5.014;使用警告;使用Image :: Magick;使用热像仪;我的$ file = shift//'qrcode.png';除非"-f $ file",否则将丢失" $ file";#Image :: Magick我的$ im = Image :: Magick-> new;$ im-> Read($ file);我的$ raw_magic = $ im-> ImageToBlob(magick =>'GRAY',depth => 8);#hexdump($ raw_magic);#成像仪我的$ img =成像仪-> new;$ img-> read(file => $ file,type =>'png')或die'read:',$ img-> errstr;我的$ gray = $ img-> convert(preset =>'gray');$ gray-> write(data => \ my $ raw_imager,type =>'raw')或die'write:',$ gray-> errstr;#hexdump($ raw_imager);如果$ raw_magic cmp $ raw_imager说不同";sub hexdump {我的$ data =移动;我的$ n;打印$ _,(++ $ n%16)?":"\ n"对于解压'(A2)*',解压'H *',$ data;打印"\ n";} 

编辑

添加一些背景信息.我想使用 Barcode :: ZBar 包.因此,使用Image :: Magick生成的原始数据调用我的 decode_qr ,QR解码器不会使用成像器的数据正确解码"hello".

  decode_qr($ raw_magic,$ im-> Get(qw(columns rows)),'magick');encode_qr($ raw_imager,$ gray-> getwidth(),$ gray-> getheight(),'imager');子decode_qr {my($ raw,$ w,$ h,$ from)= @_;path($ from.'.raw')-> spew_raw($ raw);#保存原始数据我的$ zimage =条形码::: ZBar :: Image-> new;$ zimage-> set_format('Y800');$ zimage-> set_size($ w,$ h);$ zimage-> set_data($ raw);条形码:: ZBar :: ImageScanner-> new-> scan_image($ zimage);为我的$ sym($ zimage-> get_symbols){再说join(':',$ from,$ sym-> get_type(),$ sym-> get_data());}} 

解决方案

我花了一些时间对此进行了测试,并且发生了一些奇怪的事情.我用ImageMagick创建了一个虚拟 qrcode.pnm :

 转换-size 1x1!深度8-不压缩xc:黑色xc:白色xc:灰色xc:灰色30 xc:灰色90 +附加qrcode.pnm 

因此,以下代码将按预期工作:

 #!/usr/bin/perl使用5.014;使用警告;使用Image :: Magick;使用热像仪;我的$ file = shift//'qrcode.pnm';除非"-f $ file",否则将丢失" $ file";#Image :: Magick我的$ im = Image :: Magick-> new;$ im-> Read($ file);我的$ raw_magic = $ im-> ImageToBlob(magick =>'GRAY',depth => 8);hexdump($ raw_magic);#成像仪我的$ img =成像仪-> new;$ img-> read(file => $ file,type =>'pnm')或die'read:',$ img-> errstr;我的$灰色= $ img-> convert(矩阵=> [[1,0,0]]);$ gray-> write(data => \ my $ raw_imager,type =>'raw')或die'write:',$ gray-> errstr;hexdump($ raw_imager);如果$ raw_magic cmp $ raw_imager说不同";sub hexdump {我的$ data =移动;我的$ n;打印$ _,(++ $ n%16)?":"\ n"对于解压'(A2)*',解压'H *',$ data;打印"\ n";} 

输出

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

不确定会证明什么,或者是否有用,但是明天会花费更多时间.

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 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());
        }
}

解决方案

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.

这篇关于如何从热像仪中获取Image :: Magick的Blob(原始)图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆