# 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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1652518313831/O2x_FJcR_.png)

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
