Skip to main content

Command Palette

Search for a command to run...

Bulk resize photos in Bash

Published
2 min read
Bulk resize photos in Bash

Some times We need to resize our photos.

I use bash for do it because it’s very fast and very customizable.

For example I need to resize All photos in folder “Pictures” (recursively) that names starts with “need_resize”.

This is not possible in some programs like Photoshop.

I use “convert” command from “ImageMagick” package to resize images.

Use this command to install it on

Ubuntu/Debian:

sudo apt-get install imagemagick

Redhat/Fedora/CentOS:

sudo yum install ImageMagick

The “convert” command can do this:
1. Converting images to another format

convert Image.jpg Image.png
2. Resize images

convert Image.jpg -resize 400x300 Image_resized.jpg
3. Rotate images

convert Image.jpg -rotate 180 Image_rotated.jpg

4. and etc, To know more about this command see manual page:

man convert

Examples of convert command

  1. Resize all images in current folder to 400x300:

## Not recursively

for imageName in ` ls *.{jpg,png}`

do

convert $imageName -resize 400x300 $imageName_resized

done

## Recursively

for imageName in `find ./ -type f -iname *.jpg -o -iname *.png`

do

convert $imageName -resize 400x300 $imageName_resized

done

2. Resize all images in folder that names starts with need_convert, Only change first line of above scripts to these:

## Not recursively

for imageName in ` ls need_convert*.{jpg,png}`

## Recursively

for imageName in `find ./ -type f -iname need_convert*.jpg -o -iname need_convert*.png`

alternatively You can use only find command instead for:

find ./ -type f -iname “need_convert*.jpg” -o -iname “need_convert*.png” -exec sh -c ‘fileName=”{}”; convert “$fileName” -resize 400x300 “$fileName_resized”’ \;

3. Convert all PNG files to JPEG:

for imageName in `find ./ -type f -iname “*.png”`

do

newImageName=`echo $imageName | sed ‘s/png$/jpg/i’`

convert $imageName $newImageName

done