/ Bash Scripts

Batch image conversion and resizing

Image conversion and resizing is a frequent need for web developers, graphic designers, photographers, and many others. Automating this task can save a lot of time and avoid the repetitive work of manually handling each image.

For this task, we will use ImageMagick, a powerful software suite to create, edit, compose, or convert bitmap images. It’s available for multiple platforms, including Linux, Windows, and macOS.

Before we start, make sure you have ImageMagick installed on your system. If it’s not, follow the instructions in the official ImageMagick documentation.

Step 1: Script Initialization

Begin by creating a new file for your script. You can do this with any text editor. The filename should end with .sh, indicating it’s a shell script. Let’s name ours image_process.sh.

Next, we need to define our shell interpreter using the shebang (#!) at the start of the file:

#!/bin/bash

Step 2: Define the Conversion Function

Our script will contain a function for conversion and resizing. We’ll name this function convert_resize. This function will take three arguments:

  1. The input file path.
  2. The output file format.
  3. The maximum dimension for resizing.

Here’s the skeleton of our function:

convert_resize() {
  # Our code will go here.
}

Inside this function, we’ll use the convert command from ImageMagick to convert and resize images.

The basic syntax for the convert command to resize an image is:

convert input.jpg -resize 100x100 output.jpg

This command will resize input.jpg so that its width and height are no more than 100 pixels, maintaining the aspect ratio. The resized image is saved as output.jpg.

To convert the format of an image, you just need to specify the desired extension for the output file:

convert input.jpg output.png

This command will convert input.jpg to PNG format, saving it as output.png.

So, the command to convert and resize an image is:

convert input.jpg -resize 100x100 output.png

In our function, we will replace input.jpg, 100x100, and output.png with our function’s arguments.

Here’s our function:

convert_resize() {
  input_file="$1"
  output_format="$2"
  max_dim="$3"

  # Extract the filename without the extension.
  filename=$(basename -- "$input_file")
  filename="${filename%.*}"

  # Build the output file path.
  output_file="./converted/${filename}.${output_format}"

  # Create the output directory if it doesn't exist.
  mkdir -p "$(dirname "$output_file")"

  # Perform the conversion and resizing.
  convert "$input_file" -resize "${max_dim}x${max_dim}" "$output_file"
}

Note that $(basename -- "$input_file") is used to extract the base filename without the directory, and "${filename%.*}" is used to remove the extension from the filename.

Step 3: Process All Images in a Directory

Now we have a function that can convert and resize a single image. Next, we want to apply this function to all images in a directory.

We’ll use a for loop to iterate over all JPEG and PNG files in the directory.

Here’s the code:

for img in ./images/*.jpg ./images/*.png; do
  convert_resize "$img" "jpg" 1000
done

This code will convert all JPEG and PNG images in the ./images directory to JPEG format with a maximum dimension of 1000 pixels. The converted images will be saved in the ./converted directory.

Step 4: Add Some User Interaction

To make the script more useful, let’s add some user interaction. We’ll allow the user to specify the input directory, output format, and maximum dimension as command-line arguments.

Here’s our final script:

#!/bin/bash

convert_resize() {
  input_file="$1"
  output_format="$2"
  max_dim="$3"

  filename=$(basename -- "$input_file")
  filename="${filename%.*}"
  output_file="./converted/${filename}.${output_format}"

  mkdir -p "$(dirname "$output_file")"

  convert "$input_file" -resize "${max_dim}x${max_dim}" "$output_file"
}

input_dir="$1"
output_format="$2"
max_dim="$3"

for img in "${input_dir}"/*.jpg "${input_dir}"/*.png; do
  convert_resize "$img" "$output_format" "$max_dim"
done

To use this script, the user should type:

./image_process.sh ./images jpg 1000

This command will convert all JPEG and PNG images in the ./images directory to JPEG format with a maximum dimension of 1000 pixels. The converted images will be saved in the ./converted directory.

Conclusion

With this script, you can easily automate the task of converting and resizing images. Remember to always test your script with a few images before running it on your entire collection, to make sure it’s working as expected.

This guide provides a solid foundation, but there are many ways you could enhance this script. You might add more options for the convert command, handle more types of images, or improve the error handling.

Was this helpful?

Thanks for your feedback!