Recursive Batch Image Resizing in Linux

24 01 2009

So today I needed to resize a photo archive of 3000 images spread out in sub-folders for every year and month… Fortunately, in linux with a bit of research I got it!

First off, you must download ImageMagick – if you are running Ubuntu, LinuxMint or a similar distro with APT you can simply do:

sudo apt-get install imagemagick

Then you can cd (change directory) to the folder containing the pictures that are to be resized. For example:

cd /home/username/Pictures/toShrink

Now for example, if you only need to resize the .jpgs in this folder to 800px wide do:

mogrify -resize 800 *.jpg

NOTE: This is case sensitive, .jpg is not .JPG.  It is likely that you will have to run any of these commands for all file extention variations in your collection.  For instance once with *.jpg, then *.JPG, then *.jpeg then *.JPEG

To resize them to a specific size (distorting their aspect ratio if necessary) do:

mogrify -resize 800x600! *.jpg

To resize them to 50% of their original size do:

mogrify -resize 50% *.jpg

To resize them to a specific pixel content (effectively scaling small images up and large images down to a uniform size while maintaining aspect ratio) of their original size do:

mogrify -resize 480000@ *.jpg

Basically, the above number is 800*600=480000px.  Whether it be 800×600 or 600×800 or even 1200×400 pixels in size the image contains a total of 480000 pixels.  This will scale the images so that they contain X number of pixels.

Since many of my photos were different aspect ratios, different sizes and different orientations, some even panoramas, I settled for the above option.  I used 2000000@ which made all of my 4:3 photographs 1632×1224 and the panoramas similarly sized – not totaly precise but at least now all the pictures taken by different cameras at different resolutions are all roughly the same size!

Now RECURSIVE BATCH IMAGE RESIZING:
To resize all .jpgs in this folder and every sub folder contained within it (recursively) – FIRST DO A TEST RUN! – the following command will NOT change files, it will just output to console the commands that it would be performing:

find ./ -name "*.jpg" -exec echo mogrify -resize 800 {} \;

You should see outputted to console the mogrify command for ONLY the files that you want to resize.

mogrify -resize 800 ./subFolderName/fileName.jpg 

This is an example output that I get:

mogrify -resize 800 ./2007/12_December/IMG_0172.jpg
mogrify -resize 800 ./2007/12_December/IMG_0199.jpg
mogrify -resize 800 ./2007/12_December/IMG_0166.jpg
mogrify -resize 800 ./2007/04_April/2007-04-25_ph101_12.jpg
mogrify -resize 800 ./2007/04_April/041207_13491.jpg
mogrify -resize 800 ./2007/04_April/2007-04-25_ph101_13.jpg

Make sure that the options are correct and that it is modifying the correct files.  Use caution, test it out on a few images first..  This overwrites the original file so make sure that you have a backup of everything just in case something goes wrong.
If it all looks good and you’re ready to go, simply remove the echo .
This is the final command that I used (resizing 4:3 images to around 1632×1224):

find ./ -name "*.jpg" -exec mogrify -resize 2000000@ {} \;

Remember, it is case sensitive.  Run it again for *.JPG , *.jpeg, and *.JPEG!

ImageMagick can do a whole lot more, things such as image conversions.. like JPG to PNG 🙂

convert *.jpg imageSeriesName.png

Check out the ImageMagick site to learn more about what you can do with this extremely powerful non-graphical graphics program! 😀

Good luck guys, hope this post helps make life easier for a fellow creature of the net!


Actions

Information

11 responses

1 03 2009
Mike

Just passing by.Btw, you website have great content!

_________________________________
Making Money $150 An Hour

28 07 2012
Anonymous

thanks, this was helpful

28 10 2012
masterpeter

hi mate! excellent article. just towards the end you advise to run this command separately for jpg JPG etc… when you could just use the -iname switch for “find” that would then search for matching files in an case insensitive manner.

14 01 2013
Thijs

if you add the criteria to the find command you can run it in one instance:

find ./ -name “*.jpg” -o -name “*.JPG” -o -name “*.png” -o -name “*.PNG” find ./ -name “*.jpg” -o -name “*.JPG” -o -name “*.png” -o -name “*.jpeg” etc

13 01 2014
varnak

Thank you very much. It was very helpful.

Here is improvement. You’ll see current resizing file name with path.
It is very useful if you have lot of files, like 50 000 – 100 000.
You can visualize the progress

#!/bin/bash
for i in $(find ./);
do
mogrify -resize 100000@ $i
echo $i
done

13 01 2014
varnak

Or modified script with Thijs improvements.

It will resize all JPG/jpg/JPEG/jpeg files 🙂

#!/bin/bash

for i in $(find ./ -name ‘*.jpg’ -o -name ‘*.JPG’ -o -name ‘*.jpeg’ -o -name ‘*.JPEG’);
do
mogrify -resize 100000@ $i
echo $i
done

4 07 2014
mc2027

Have you had any luck doing the above with convert or mogrify? I tried:

find ./ -name “*.tif” -exec mogrify -format pdf *.tif {} \;

(convert a bunch of subdirectories full of TIF files to PDFs). I had no luck and keep getting a find: no match error.

29 01 2016
marswilliams

This article saved me tons of time! Thanks so much!

28 01 2017
stavrosa

recursively, yes! thanks for that

FOR DEMO
find -name “*.JPG” -exec echo “mogrify -monitor -resize ‘1400×1400>'” {} \;

FOR FINAL
find -name “*.JPG” -exec mogrify -monitor -resize ‘1400×1400>’ {} \;

beware the double quotes on echo!!

1 02 2018
Richard Landaverde

Great artitcl

1 02 2018
Richard Landaverde

Great Article. Definitely saved me a lot of time.

Leave a comment