ImageMagick - Resize all images in a directory

I need to resize all images in a directory. The convert command from ImageMagick combined with a loop over all image files in a directory seems to be a good solution.

convert command

The usage of the convert command for resizing on a single image can look like this.

convert image_file.png -resize 300x300 image_file_resize.png

The command from above will resize the image image_file.png with the same aspect ratio to a maximum size of 300x300 and will write the result into the file image_file_resize.png.

loop over all files

Here a example of a bash script that just prints all files with extension png in a directory.

#!/bin/bash

for i in *.png; do
    printf "$i\n"
done

Let us assume that we have a file structure like the following one.

image_dir/
|-- 1.png
|-- 2.png
|-- 3.png
|-- 4.png
`-- script

The output of the script would look like this.

bash script
1.png
2.png
3.png
4.png

Resize images in a loop

We can simply combine the stuff from above to the following script.

#!/bin/bash

for i in *.png; do
    printf "Resize $i\n"
    convert "$i" -resize 300x300 "$i"
done

We can execute the script the same way as we did before.

bash script
Resize 1.png
Resize 2.png
Resize 3.png
Resize 4.png
Next Previous