[英]Perl Image::Magick get in-memory contents
I'm using Image::Magick
to modify my images. 我正在使用
Image::Magick
修改我的图像。 I am then using an HTTP::Request
to send the image content to an API. 然后,我使用
HTTP::Request
将图像内容发送到API。
HTTP::Request
has a content
method which allows you to set the content for the request, but obviously this requires that you have the content in memory. HTTP::Request
具有一个content
方法,该方法允许您设置请求的内容,但是显然,这需要您将内容存储在内存中。
I know that I can read the content of the image into a variable by opening a file and reading it. 我知道我可以通过打开文件并读取将图像的内容读入变量。 However, since
Image::Magick
already has the content of the image in memory, is there any way that I can get it via my Image::Magick
object? 但是,由于
Image::Magick
已经在内存中存储了图像的内容,是否有任何方法可以通过Image::Magick
对象获取它? Thanks! 谢谢!
Image::Magick
keeps the image in a custom format in memory to make it more simple and efficient to manipulate. Image::Magick
将图像以自定义格式保留在内存中,以使其更简单有效地进行操作。 It has to compress the data to JPEG, PNG, or whatever format you request before it is written to a file, so you cannot just access the image in memory as it stands. 在将数据写入文件之前,它必须将数据压缩为JPEG,PNG或任何您要求的格式,因此您不能仅访问内存中的图像。
However, the module's ImageToBlob
method will provide an in-memory copy of the data it would have written to disk, to save you writing it out and reading it back again. 但是,该模块的
ImageToBlob
方法将提供已写入磁盘的数据的内存中副本,以免您将其写出并再次读回。
Note that it returns a list of images to allow for an object that contains more than one frame, so if you have only a single frame you must write 请注意,它返回图像列表以允许一个对象包含多个框架,因此,如果只有一个框架,则必须编写
my @blobs = $image->ImageToBlob;
$request->content($blobs[0]);
or 要么
my ($blob) = $image->ImageToBlob;
$request->content($blob);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.