我想用ImageMagick压缩一个JPG图像文件,但在大小上不能得到太大的差异。默认情况下,输出大小大于输入。我不知道为什么,但添加了一些+配置文件选项和设置质量后,我可以得到一个较小的尺寸,但仍然类似于原始。
输入图像为255kb,处理图像为264kb(使用+profile删除profile,设置质量为70%)。有没有办法将图像压缩到至少150kb ?这可能吗?我可以使用哪些ImageMagick选项?
我想用ImageMagick压缩一个JPG图像文件,但在大小上不能得到太大的差异。默认情况下,输出大小大于输入。我不知道为什么,但添加了一些+配置文件选项和设置质量后,我可以得到一个较小的尺寸,但仍然类似于原始。
输入图像为255kb,处理图像为264kb(使用+profile删除profile,设置质量为70%)。有没有办法将图像压缩到至少150kb ?这可能吗?我可以使用哪些ImageMagick选项?
当前回答
如果图像有很大的尺寸,不调整大小很难得到好的结果,下面是60%的调整大小,对于大多数目的不会破坏太多的图像。
我用这个对灰度图像有很好的效果(我从PNG转换过来):
ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -adaptive-resize 60% -gaussian-blur 0.05 -colorspace Gray -quality 20 {}.jpg
我使用这个扫描的B&W页面,让他们到灰度图像(额外的参数清除阴影从以前的页面):
ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -adaptive-resize 60% -gaussian-blur 0.05 -colorspace Gray -quality 20 -density 300 -fill white -fuzz 40% +opaque "#000000" -density 300 {}.jpg
我在彩色图像中使用这个:
ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -adaptive-resize 60% -gaussian-blur 0.05 -colorspace RGB -quality 20 {}.jpg
其他回答
有一次我需要调整相机照片的大小来冲洗:
原始文件大小: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%,但质量更好(缩放时)。
我在建议的命令中添加了-adaptive-resize 60%,但添加了-quality 60%。
convert -strip -interlace Plane -gaussian-blur 0.05 -quality 60% -adaptive-resize 60% img_original.jpg img_resize.jpg
这是我的结果
img_original.jpg = 13,913KB jpg = 845KB
我不确定这种转变是否严重破坏了我的形象,但说实话,我不认为我的转变看起来像垃圾。这是一个广角全景,我不介意一丝不苟的阻碍。
我使用谷歌Pagespeed Insights图像优化指南,对于ImageMagick,他们推荐以下:
-sampling-factor 4:2:0 带 -quality 85[它可以变化,我使用范围60-80,这里数字越小文件越小] 交错 色彩RGB
ImageMagick中的命令:
convert image.jpg -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB image_converted.jpg
使用这些选项,我可以节省高达40%的JPEG大小,而没有太多明显的损失。
我用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);
}
}