Bulk convert lots of images from one folder to another Shell Script loop

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
pbul2004
Posts: 8
Joined: 2018-02-14T04:09:23-07:00
Authentication code: 1152

Bulk convert lots of images from one folder to another Shell Script loop

Post by pbul2004 »

Hi,

I have been having issues with the page speed of my site, and after a lot of research i am hoping Image Magick will sort it out.

I want to bulk convert lots of images in one folder then export all the images into an output folder folder.

Is this possible with a shell script? ie: Loop through all images from /input run the script below then output the optimised images in /output?

Here is what I have so far but this is doing one image at a time:

Code: Select all

convert /Users/bob/Desktop/image-magick/santorini.jpg -sampling-factor 4:2:0 -strip -quality 75 -resize 700x466! -interlace JPEG -colorspace RGB -background white -flatten /Users/bob/Desktop/image-magick/final/santorini-opt.jpg 
Here is the report from my site of the errors i am trying to fix:
https://gtmetrix.com/reports/www.tinker ... m/ac0dMCTe

Any help would be great. I am very new to this, so if there is also anything I am missing or should add/remove to my script please let me know.

I am using command line on a mac.

Thanks
Paul
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by fmw42 »

A shell script is possible. You should strip before setting any settings or operators. If on current IM, then use -colorspace sRGB if you want non-linear color and RGB if you want linear color. If on very old IM versions then reverse them.

Code: Select all

cd path2/inputdirectory
list=$(ls *.jpg)
for img in $list; do
inname=$(convert -ping $img -format "%t" info:)
convert $img -strip -colorspace RGB -resize 700x466! -background white -flatten -interlace JPEG -sampling-factor 4:2:0 -quality 75 path2/outputdirectory/${inname}.jpg
done
pbul2004
Posts: 8
Joined: 2018-02-14T04:09:23-07:00
Authentication code: 1152

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by pbul2004 »

Hi, thanks for the reply

I keep getting the following message:

convert: unable to open image 'output/IMG_1297.JPG': No such file or directory @ error/blob.c/OpenBlob/3335.

I have altered the code to match my directory setup:

Code: Select all

#!/bin/bash

cd input
list=$(ls *.JPG)
for img in $list; do
inname=$(convert -ping $img -format "%t" info:)
convert $img -strip -colorspace RGB -resize 700x466! -background white -flatten -interlace JPEG -sampling-factor 4:2:0 -quality 75 output/${inname}.jpg
done

echo Success: Optimised images in /output
I only recently installed Image Magick so i think ill be on the latest version. I'm an obvious newbie to this, so not sure what that means about the linear etc. Are the settings i have got right to best optimise my images for best compression and quality!

Hope you can help :-)

Cheers
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by snibgo »

IM won't create directories. Your command assumes that "output" is a subdirectory of "input". If that isn't true, the convert will fail with that error message.
snibgo's IM pages: im.snibgo.com
pbul2004
Posts: 8
Joined: 2018-02-14T04:09:23-07:00
Authentication code: 1152

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by pbul2004 »

Ah ok. Is it possible for the two folders to be seperate and not within input:

/test/run.sh (shellscript)
/test/input
/test/output
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by snibgo »

That's what ".." is for. Your output filename can be:

Code: Select all

../output/${inname}.jpg
That means "go up a level from the current directory, and look for "output" in that directory".
snibgo's IM pages: im.snibgo.com
pbul2004
Posts: 8
Joined: 2018-02-14T04:09:23-07:00
Authentication code: 1152

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by pbul2004 »

yeah got it, thanks for the help guys! All up and running.

Do i have the best settings for optimal compression and quality? Should i be using "sRGB" instead? Also the -sampling-factor 4:2:0, is that all ok?

Finally can you resize based on a % ratio rather than an exact size, like: -resize "700x466!"
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by snibgo »

pbul2004 wrote:... optimal compression and quality?
You can optimize one of the other but not both. They trade off each other.

Personally, I use two methods. When I don't really care about the results, I use "-quality 40". That's generally good enough quality, and very small.

When I do care about the results (eg my web pages) I iterate through quality settings to find the lowest quality (ie best compression) that is no more than 1% RMSE from the original image.

I've never played with "-sampling-factor" or "-interlace".

EDIT: Oops, I've just noticed your "-colorspace RGB". Why do you have that? If you use a recent version of IM, that is wrong.
snibgo's IM pages: im.snibgo.com
pbul2004
Posts: 8
Joined: 2018-02-14T04:09:23-07:00
Authentication code: 1152

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by pbul2004 »

I am using Version: ImageMagick 7.0.7-22 Q16 x86_64 2018-02-01

The colorspace RGB was something i found from an example, was never too sure if i actually needed or what it did.

Again, same with "-sampling-factor" or "-interlace". < Not sure if i even need these or what they do! They just got recommended on a post i found.

The images on my site are travel related, so i would like them looking sharp: https://www.tinkertravels.com/

However when i run GTMetrix scan the images are really bad : https://gtmetrix.com/reports/www.tinker ... m/ac0dMCTe

So hoping this will stop it complaining and fix them
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by snibgo »

pbul2004 wrote:The colorspace RGB was something i found from an example, was never too sure if i actually needed or what it did.
It makes your images linear, which is a really bad idea for the web. (In very old versions of IM, many years ago, it had the reverse effect).
snibgo's IM pages: im.snibgo.com
pbul2004
Posts: 8
Joined: 2018-02-14T04:09:23-07:00
Authentication code: 1152

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by pbul2004 »

snibgo wrote: 2018-02-14T13:22:19-07:00 It makes your images linear, which is a really bad idea for the web. (In very old versions of IM, many years ago, it had the reverse effect).
Should i totally remove this, or change it to sRGB?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by fmw42 »

It is best to keep it as -colorspace sRGB in case you have CMYK input images. Alternately, you would have to use profiles to change any CMYK inputs to sRGB outputs.
pbul2004
Posts: 8
Joined: 2018-02-14T04:09:23-07:00
Authentication code: 1152

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by pbul2004 »

@fmw42 thanks.

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, any advice would be great, as id like to understand what it is i am doing :-)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bulk convert lots of images from one folder to another Shell Script loop

Post by fmw42 »

You can resize using %. So -resize 50x75% would resize to 50% width and 75% of height. http://imagemagick.org/script/command-l ... p#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. http://imagemagick.org/script/command-l ... #interlace and http://imagemagick.org/script/command-l ... ling-facto
Post Reply