Speed Up Script

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
FUrn
Posts: 23
Joined: 2018-10-30T09:49:10-07:00
Authentication code: 1152

Speed Up Script

Post by FUrn »

I use the following code to go through each image in a folder, and resize the canvas so that it is square (i.e. by taking the longest side and applying that to the shorter side):

Code: Select all

for filename in *.jpg; do
magick "$filename" -gravity Center -background White -extent "%[fx:max(w,h)]x%[fx:max(w,h)]" "$filename"
done
With 40,000+ images to process, this has taken me a significant number of hours - I'm looking for help to speed this up? I was thinking that before processing each image, I could add a check to see if the image is already square and if it is then skip to the next image - would that allow me to work through the images faster? If so I'd be grateful if someone could help me out with the code needed for this check (or other ideas to speed this up).

I'm using IM 7 on a Windows PC.

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

Re: Speed Up Script

Post by fmw42 »

Why don't you just test your proposal? Your code is Unix, so I assume you are using Windows 10 Unix and not normal Windows.

for filename in *.jpg; do
test=$(magick "$filename" -format "%[fx:(w==h)?1:0]" info:)
if [ $test -eq 0 ]; then
magick "$filename" -gravity Center -background White -extent "%[fx:max(w,h)]x%[fx:max(w,h)]" "$filename"
fi
done

FUrn
Posts: 23
Joined: 2018-10-30T09:49:10-07:00
Authentication code: 1152

Re: Speed Up Script

Post by FUrn »

Thanks Fred! That code worked a treat :)
I'm a real beginner when it comes to Unix code (and code in general), hence why I wasn't able to write up the test myself.
In any case it seems to have worked...on a sample of 100 images, the original code took 35 seconds to run whilst the code with a check took 22 seconds...if those results bear out when processing the 40,000+ images, that will make quite the difference!

Post Reply