避免图像的双重优化 [英] Avoid double optimization of image

查看:86
本文介绍了避免图像的双重优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们存储了一些图像,其中一些图像已经过优化,而有些则没有.

We have a storage of images where some images are optimized and some are not.

我正在研究脚本",它将遍历存储中的每个图像并运行优化过程.

I'm working on 'script' that will go via every image in the storage and run optimize process.

我想知道:

  1. 是否可以检查图像是否已优化?
  2. 如果将图像质量进行85%的优化几次,我会失去质量吗?

我尝试在同一张图像上几次运行-quality 85%,但我看不到任何质量损失(第3次运行后,图像的大小未更改).但是我没有找到官方文件的证明.

I have tried to run -quality 85% few times on same image and I could not see any lose in quality (after 3-th run the image's size was not changed). However I did not find a proof on official documentation.

谢谢!

推荐答案

您可以在优化之前检查质量设置是否已经为75:

You can check if the quality setting is already 75 before optimising:

identify -format %Q fred.jpg
92

然后优化并再次检查:

convert fred.jpg -quality 75 optimised.jpg
identify -format %Q optimised.jpg
75

如果您使用的是bash,这很容易:

If you are using bash, this is easy:

q=$(identify -format %Q fred.jpg)
[ $q -ne 75 ] && mogrify -quality 75 fred.jpg


将图像标记为已优化的另一种方法可能是在文件中将注释设置为单词""已优化" ,如下所示:


Another way to mark images as optimised might be to set a comment in the file to the word "optimised", like this:

# Set comment
mogrify -set comment "optimised" fred.jpg

# Get comment
identify -format %c fred.jpg 
optimised

因此,您将测试图像注释是否包含单词""已优化" ,如果不是,请优化图像并将注释更改为尽可能显示:

So, you would test if an image comment contains the word "optimised", and if not, optimise the image and change the comment to show as much:

[[ $(identify -format %c fred.jpg) != *optimised* ]] && { echo Optimising fred.jpg; mogrify -set comment "optimised" -quality 75 fred.jpg; }


如果要处理成百上千个图像,建议使用 GNU Parallel .请尝试对目录中一些文件的小副本使用以下示例:


If you had hundreds or thousands of images to process, I would suggest GNU Parallel. Please try the example below on a small copy of a few files in a directory:

parallel '[[ $(identify -format "%c" {}) != *optimised* ]] && { echo Optimising {}; mogrify -set comment "optimised" -quality 75 {}; }' ::: *jpg

您会发现它将在第一遍并行处理所有对象,然后在第二遍不执行任何操作.

You will find it will process them all in parallel the first pass, and then do nothing on the second pass.

如果图像太多,并且出现错误:参数过多" ,则可以像这样在stdin上传递文件名,而不是在:::之后进行遍历:

If you have too many images, and get "ERROR: Too many args", you can pass the filenames in on stdin like this, instead of globbing after the ::::

find /some/where -iname \*.jpg -print0 | parallel -0 ...

这篇关于避免图像的双重优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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