0

I want to batch convert / loop through all images within the folder "/input" and then optimise all the images and finally output them in the folder "/output".

How can I do something similar to this:

convert /Users/james/Desktop/image-magick/santorini.jpg -sampling-factor 4:2:0 -strip -quality 75 -resize 700x466! -interlace JPEG -colorspace RGB -background white -flatten /Users/james/Desktop/image-magick/final/santorini-opt.jpg 

^ Currently with the above script, I have to run manually for every image, one at a time!

How can I do something similar using shell script to do it all for me in bulk?

fmw42
  • 46,825
  • 10
  • 62
  • 80
John
  • 1,777
  • 9
  • 42
  • 66

2 Answers2

3

You should use mogrify command instead of convert. Use -path option to create output images in a specific directory. If you don't, your original images will be overwritten.

mogrify -sampling-factor 4:2:0 -strip -quality 75 -resize 700x466! -interlace JPEG -colorspace RGB -background white -flatten -path /Users/james/Desktop/image-magick/final/ /Users/james/Desktop/image-magick/*.jpg
FredG
  • 276
  • 2
  • 10
  • Tried to copy your script and run it (changed the paths), but i get: unable to open image './input/*.jpg': No such file or directory @ error/blob.c/OpenBlob/3335. – John Feb 14 '18 at 15:07
  • @John strange, I tried it on a bunch of .jpg files without issues. Are you sure there are any .jpg files into the ./input/ directory ? Did you run it in a terminal or through a shell script ? – FredG Feb 14 '18 at 15:11
  • through terminal calling the shell script. Yes images are in the input folder – John Feb 14 '18 at 15:13
  • Which OS do you use ? – FredG Feb 14 '18 at 15:15
  • I use MAC Yoesmite – John Feb 14 '18 at 15:16
  • You may first try to launch the command directly from your `./input/` folder with `*.jpg` final argument – FredG Feb 14 '18 at 15:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165164/discussion-between-fredg-and-john). – FredG Feb 14 '18 at 15:21
2

The previous answer is almost correct. Also use -strip first before any other settings or they will be lost. Also current versions of ImageMagick use -colorspace sRGB unless you want the darker linear RGB colorspace. The following has proper ImageMagick syntax to strip, convert to sRGB, resize and then set all the jpg output settings.

cd path2/inputdirectory
mogrify -strip -colorspace sRGB -resize "700x466!" -background white -flatten -interlace JPEG -sampling-factor 4:2:0 -quality 75 -path /Users/james/Desktop/image-magick/final/ *.jpg


Note that JPG does not support transparency, so I am not sure why you have -background white -flatten in the command. That would be appropriate if your input were png (i.e. *.png or just * to handle all formats)

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • ah right, thanks - i wasn't too sure what half the commands i had were doing. I will remove the bg white and flatten. Above works perfectly and saves and additional 20KB. Thanks :-) – John Feb 14 '18 at 18:16
  • just wanted to ask, is there a way to resize by a % based ration or something rather than specifying an exact size? Also what does -interlace do and -sampling-factor 4:2:0 not sure if these settings are right or what they actually do? – John Feb 14 '18 at 20:33
  • You can resize using %. So -resize 50x75% would resize to 50% width and 75% of height. https://www.imagemagick.org/script/command-line-processing.php#geometry If you want to do that exactly and not preserve aspect, you can add ! to the command, but it might distort the image. Interlace means the image will be drawn in interlace format, like window blinds as it displays. -sampling factor changes the ratio of YUV JPG channel compressions. https://www.imagemagick.org/script/command-line-options.php#interlace and https://www.imagemagick.org/script/command-line-options.php#sampling-factor – fmw42 Feb 14 '18 at 20:53