ImageMagick的Perl API创建非常大的JPEG缩略图

发布于 2025-02-10 06:02:53 字数 2488 浏览 4 评论 0原文

我正在使用ImageMagick的Perl API来创建缩略图。我正在使用ImageMagick上的建议

open my $file, $params->{'openFile'};
my $imageData = do { local $/; <$file> };

if ($params->{'size'} eq "largesized") {
    $thumbnailWidth = 1000;
    $thumbnailHeight = 1000;
}   

$image->BlobToImage($imageData);
$image->SetAttribute('quality' => 80);
$image->SetAttribute('compression' => 'JPEG');
$image->SetAttribute('sampling-factor' => '4:2:0');
$image->SetAttribute('interlace' => 'Plane');
$image->SetAttribute('gaussian-blur' => '0.05');
$image->Thumbnail('geometry' => $thumbnailWidth . "x" . $thumbnailHeight);

my $thumbnailData = $image->ImageToBlob();


open(my $file, '>', $params->{'saveFile'}) or return { 'thumbnailGenerated' => 0, error => "Could not open file '" . $params->{'saveFile'} . "'." };
print $file $thumbnailData;
close $file;

据我所知,这应该创建一个精心优化的缩略图。但是,尽管输入图像为3088x2320,重量为1.6 Mb ,,由1000x1000 实际是2.3 MB的较大(似乎对A似乎很大,对于A似乎很大,对于A似乎很大该大小的JPEG)。注意:在样本中,它被裁剪了一点,因为上传图像的JavaScript迫使一个人逐渐设计1:1裁剪。

我正在运行ImageMagick 6.9.10-68 Q16 X86_64 2021-10-14(CENTOS)。命令行等效量按预期产生较小的图像(84KB):

  convert -thumbnail 1000x1000 -quality 80 -compress JPEG -sampling-factor 4:2:0 -interlace Plane -gaussian-blur 0.05  imageMagickSampleImage.jpg imageMagickSampleImage3.jpg

这让我想知道setAttribute属性是否未传递给thumbnail函数或其他东西。我发现Perlmagick在各个方面都不是完美的,并且一直想知道我是否只需要使用gd

我注意到缩略图仍然太大(并且比命令行还大),但是如果我删除所有“ setAttribute”线,则会变小。 1000x1000缩略图的输出从2.4MB到976KB。与这些相同属性集的命令行大于命令行高10倍。

我认为也许将某些属性直接移至ImageToblob方法会有所帮助,但似乎根本没有改变结果。这就是尝试:

my $thumbnailData = $image->ImageToBlob('quality' => 40, 'compression' => 'JPEG', 'sampling-factor' => '4:2:0', 'interlace' => 'Plane', 'gaussian-blur' => '0.05');

I'm using ImageMagick's Perl API to create thumbnails. I'm using suggestions on ImageMagick image optimization from this previous SO question:

open my $file, $params->{'openFile'};
my $imageData = do { local $/; <$file> };

if ($params->{'size'} eq "largesized") {
    $thumbnailWidth = 1000;
    $thumbnailHeight = 1000;
}   

$image->BlobToImage($imageData);
$image->SetAttribute('quality' => 80);
$image->SetAttribute('compression' => 'JPEG');
$image->SetAttribute('sampling-factor' => '4:2:0');
$image->SetAttribute('interlace' => 'Plane');
$image->SetAttribute('gaussian-blur' => '0.05');
$image->Thumbnail('geometry' => $thumbnailWidth . "x" . $thumbnailHeight);

my $thumbnailData = $image->ImageToBlob();


open(my $file, '>', $params->{'saveFile'}) or return { 'thumbnailGenerated' => 0, error => "Could not open file '" . $params->{'saveFile'} . "'." };
print $file $thumbnailData;
close $file;

As far as I can tell, this should create a nicely optimized thumbnail. However, while the input image is 3088x2320 and weighs in at 1.6 MB, the resulting thumbnail at 1000x1000 actually is larger at 2.3 MB (which seems positively huge for a JPEG of that size). Note: on the sample out, it is cropped a bit because the JavaScript that uploads the images forces one to select a 1:1 crop by design.

I'm running ImageMagick 6.9.10-68 Q16 x86_64 2021-10-14 (CentOS). The command line equivalent produces a smaller image as expected (84kb):

  convert -thumbnail 1000x1000 -quality 80 -compress JPEG -sampling-factor 4:2:0 -interlace Plane -gaussian-blur 0.05  imageMagickSampleImage.jpg imageMagickSampleImage3.jpg

It makes me wonder if the SetAttribute attributes are not being passed to the Thumbnail function or something. I've found PerlMagick less than perfect at various points and have been wondering if I just need to go with GD instead.

I notice that the thumbnails are still too large (and larger than the command line), but get smaller if I remove all of the "SetAttribute" lines. The output of the 1000x1000 thumbnail goes from 2.4MB to 976KB. That's still over 10x larger than the command line with these same attributes set.

I thought maybe moving some of the attributes directly to the ImageToBlob method would help, but it didn't seem to change the outcome at all. Here's that attempt:

my $thumbnailData = $image->ImageToBlob('quality' => 40, 'compression' => 'JPEG', 'sampling-factor' => '4:2:0', 'interlace' => 'Plane', 'gaussian-blur' => '0.05');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾夏年华 2025-02-17 06:02:53

问题显然是,尽管指定了JPEG压缩并输入JPEG,但如上所述,ImageToblob方法正在输出PNG文件,如上所述。

一个有用的代码段,来自 提供解决方案。我对代码进行了相应的修改,以专门告诉ImageMagick输出JPEG:

 my $thumbnailData = $image->ImageToBlob(magick => 'jpeg');

1000x1000图形从以前的大小变为167KB。

The issue apparently is that despite specifying JPEG compression and inputting a JPEG, the ImageToBlob method was outputting a PNG file, as @MarkSetchell observed in the comments above.

A helpful code snippet from Perl Graphics Programming offers the solution. I modified my code accordingly to specifically tell ImageMagick to output a JPEG:

 my $thumbnailData = $image->ImageToBlob(magick => 'jpeg');

The 1000x1000 graphic went from its previously massive size to just 167KB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文