如何在Image :: Magick :: Read()中使用IO :: Scalar? [英] How can I use IO::Scalar with Image::Magick::Read()?

查看:126
本文介绍了如何在Image :: Magick :: Read()中使用IO :: Scalar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 GD :: Image 操纵过的图像我想做使用 Image :: Magick 进一步操作。我想避免将图像写入磁盘,因此Image :: Magick可以读取它.Image :: Magick的Read函数将接受一个文件句柄作为参数,所以我试图将它传递给IO :: Scalar我使用GD :: Image的输出创建的对象。

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.

但是,由于IO :: Scalar对象可以被视为字符串,它看起来像Image :: Magick将图像的内容解释为无法找到的文件名,Read()失败。

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:

$response is:

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


推荐答案

我认为您正在寻找 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__

这篇关于如何在Image :: Magick :: Read()中使用IO :: Scalar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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