我想用ImageMagick压缩一个JPG图像文件,但在大小上不能得到太大的差异。默认情况下,输出大小大于输入。我不知道为什么,但添加了一些+配置文件选项和设置质量后,我可以得到一个较小的尺寸,但仍然类似于原始。

输入图像为255kb,处理图像为264kb(使用+profile删除profile,设置质量为70%)。有没有办法将图像压缩到至少150kb ?这可能吗?我可以使用哪些ImageMagick选项?


当前回答

我用always:

质量在85 渐进(复合压缩) 一个非常小的高斯模糊来优化大小(半径的0.05或0.5)取决于图片的质量和大小,这明显优化了jpeg的大小。 剥离任何注释或EXIF元数据

在形象魔术应该是

convert -strip -interlace Plane -gaussian-blur 0.05 -quality 85% source.jpg result.jpg

或者在新版本中:

magick source.jpg -strip -interlace Plane -gaussian-blur 0.05 -quality 85% result.jpg

源。

From @Fordi in the comments (Don't forget to upvote him if you like this): If you dislike blurring, use -sampling-factor 4:2:0 instead. What this does is reduce the chroma channel's resolution to half, without messing with the luminance resolution that your eyes latch onto. If you want better fidelity in the conversion, you can get a slight improvement without an increase in filesize by specifying -define jpeg:dct-method=float - that is, use the more accurate floating point discrete cosine transform, rather than the default fast integer version.

其他回答

我用always:

质量在85 渐进(复合压缩) 一个非常小的高斯模糊来优化大小(半径的0.05或0.5)取决于图片的质量和大小,这明显优化了jpeg的大小。 剥离任何注释或EXIF元数据

在形象魔术应该是

convert -strip -interlace Plane -gaussian-blur 0.05 -quality 85% source.jpg result.jpg

或者在新版本中:

magick source.jpg -strip -interlace Plane -gaussian-blur 0.05 -quality 85% result.jpg

源。

From @Fordi in the comments (Don't forget to upvote him if you like this): If you dislike blurring, use -sampling-factor 4:2:0 instead. What this does is reduce the chroma channel's resolution to half, without messing with the luminance resolution that your eyes latch onto. If you want better fidelity in the conversion, you can get a slight improvement without an increase in filesize by specifying -define jpeg:dct-method=float - that is, use the more accurate floating point discrete cosine transform, rather than the default fast integer version.

下面是在PHP中使用Imagick的完整解决方案:

$im = new \Imagick($filePath);
$im->setImageCompression(\Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(85);
$im->stripImage();
$im->setInterlaceScheme(\Imagick::INTERLACE_PLANE);

// Try between 0 or 5 radius. If you find radius of 5 
// produces too blurry  pictures decrease to 0 until you 
// find a good balance between size and quality. 
$im->gaussianBlurImage(0.05, 5);



// Include this part if you also want to specify a maximum size for the images

$size = $im->getImageGeometry();
$maxWidth = 1920;
$maxHeight = 1080;


// ----------
// |        |
// ----------
if($size['width'] >= $size['height']){
  if($size['width'] > $maxWidth){
    $im->resizeImage($maxWidth, 0, \Imagick::FILTER_LANCZOS, 1);
  }
}


// ------
// |    |
// |    |
// |    |
// |    |
// ------
else{
  if($size['height'] > $maxHeight){
    $im->resizeImage(0, $maxHeight, \Imagick::FILTER_LANCZOS, 1);
  }
}

有一次我需要调整相机照片的大小来冲洗:

原始文件大小:2800 kB 分辨率:3264 x2448

命令:

mogrify -quality "97%" -resize 2048x2048 -filter Lanczos -interlace Plane -gaussian-blur 0.05 

结果文件大小为753 kB 2048号决议高模

我用1920x1080分辨率的显示器全屏看不出任何变化。2048分辨率是最好的10厘米照片在360 dpi的最大质量。我不想剥了它。

编辑:我注意到,如果没有模糊,我甚至得到了更好的结果。没有模糊的文件大小是原始的50%,但质量更好(缩放时)。

对于那些在PHP中使用Imagick类的人

$im -> gaussianBlurImage(0.8, 10);      //blur
$im -> setImageCompressionQuality(85);  //set compress quality to 85

@JavisPerez——有什么方法可以把图片压缩到至少150kb ?是, 可能吗?我可以使用哪些ImageMagick选项?

请参阅以下链接,其中ImageMagick中有一个选项,用于指定写入JPG文件所需的输出文件大小。

http://www.imagemagick.org/Usage/formats/#jpg_write http://www.imagemagick.org/script/command-line-options.php#define

——jpeg:程度上={大小} 从IM v6.5.8-2开始,您可以为JPEG图像指定最大输出文件大小。大小由后缀指定。例如“400kb”。

convert image.jpg -define jpeg:extent=150kb result.jpg

通过解压和重新压缩,除了由于降低输入的质量值而造成的损失外,还会损失一些质量。