ImageMagick script to resize folder of images

2019-08-11 08:36发布

问题:

I have a folder of images of varying sizes and quality. Is there a way with ImageMagic that I could automatically resize them to be no bigger 1100px x 1100px and less than 160kb. And not to re-size if they are smaller than those parameters. Also not to distort the image so it fits within but to only re-size so for example an image which is 2200px by 1000px would become 1100px by 500px.

I'm working on Ubuntu with ImageMagick 6.7.7-10.

回答1:

Try this on a copy of your files:

mogrify -define jpeg:extent=160k -resize 1100x1100\> *.jpg

I am assuming your files are JPEG images. The command will largely work for other image types too, but it can only enforce the 160kB limit for JPEG files - not PNG, or TIFF etc.

Example

With version: ImageMagick 6.9.1-10 Q16 x86_64 2015-08-06

convert -size 2000x2000 xc:gray +noise random a.jpg

ls -lrt a.jpg
-rw-r--r--  1 mark  staff  6969601 21 Aug 18:28 a.jpg            # <--- 7MB

mogrify -define jpeg:extent=160k -resize 1100x1100\> a.jpg

ls -lrt a.jpg
-rw-r--r--  1 mark  staff  147938 21 Aug 18:28 a.jpg             # <--- 160kB

identify a.jpg
a.jpg JPEG 1100x1100 1100x1100+0+0 8-bit sRGB 148KB 0.000u 0:00.000