How to batch resize images in Ubuntu recursively within the terminal?
-
11-06-2021 - |
Question
I have multiple images stored in a set of organized folders. I need to re-size those images to a specific percentage recursively from their parent directory. I am running Ubuntu 11.10 and i prefer learning how to do that directly from the terminal.
Solution
You could use imagemagick. For instance, for resizing all the JPG images under the current directory to 50% of their original size, you could do:
for f in `find . -name "*.jpg"`
do
convert $f -resize 50% $f.resized.jpg
done
The resulting files will have ".jpg" twice in their names. If that is an issue, you can check the following alternatives.
For traversing/finding the files to resize, you can use xargs too. Example:
find . -name "*.jpg" | xargs convert -resize 50%
This will create copies of the images. If you just want to convert them in place, you can use:
find . -name "*.jpg" | xargs mogrify -resize 50%
OTHER TIPS
Extending the answer from @betabandido
Incase there are spaces in filenames or folder names in which the images are, then one should use -print0 with find and -0 with xargs to avoid any parsing errors.
find . -name "*.jpg" -print0 | xargs -0 convert -resize 50%
find . -name "*.jpg" -print0 | xargs -0 mogrify -resize 50%
You can also use
sudo apt install imagemagick
sudo apt-get install nautilus-image-converter
nautilus -q
For resizing/rotating images in the current folder. You just install and then right click on an image or multiple ones and choose the size you want and that's it. The nautilus -q
is to stop nautilus. Just start nautilus again, and you'll be able to use the image converter.
It's also works if you give the new resize resolution :
convert $f.jpg -size 1024x768 $f.resized.png
You can use imagemagick tool for batch resize.
It will maintain the aspect ratio
$ convert dragon.gif -resize 64x64 resize_dragon.gif
It will not maintain the aspect ratio
$ convert dragon.gif -resize 64x64\! exact_dragon.gif
$ cat resize.sh
#!/bin/bash
for f in `find . -name "*.jpg"`
do
convert $f -resize 45x60\! $f.resize.jpg
done
It will resize the image to 45x60 without maintaining the aspect ratio in current directory.
there are a few answers like:
find . -name "*.jpg" | xargs convert -resize 50%
this won't work as it will expand the list like this:
convert -resize 50% a.jpg b.jpg c.jpg
which will resize a.jpg
in c-0.jpg
, b.jpg
in c-1.jpg
and let c.jpg
untouched.
So you have to execute the resize command for each match, and give both input file name and output file name, with something like:
find . -name "*.jpg" | xargs -n 1 sh -c 'convert -resize 50% $0 $(echo $0 | sed 's/\.jpg/-th\.jpg/')'
each match of find
is individually passed by xargs -n 1
to the resize script: sh -c 'convert -resize 50% $0 $(echo $0 | sed 's/\.jpg/-th\.jpg/')'
.
This script receives the file name in argument $0
, uses sed
to make an output file name by substitution of the original .jpg
suffix by a -th.jpg
one.
And it runs the convert
command with those two file names.
Here is the version without xargs
but find -exec
:
find -name '*.jpg' -exec sh -c 'convert -resize 50% $0 $(echo $0 | sed 's/\.jpg/-th\.jpg/')' {} \;