Bash Programming

bash script: Bulk Image Resizer Script


#!/bin/bash

# Check if the required arguments are provided
if [ $# -ne 3 ]; then
echo "Usage: $0 "
exit 1
fi

input_dir=$1
output_dir=$2
resolution=$3

# Create the output directory if it does not exist
mkdir -p $output_dir

# Iterate over each file in the input directory
for file in $input_dir/*; do
if [ -f "$file" ]; then
filename=$(basename $file)
output_file=$output_dir/$filename

# Resize the image while maintaining aspect ratio
convert $file -resize $resolution $output_file
echo "Resized $file to $resolution"
fi
done

echo "Image resizing complete."

This Bash script is designed to resize a batch of images to a specified resolution while maintaining the aspect ratio.

The script first checks if the required arguments are provided – the input directory, output directory, and resolution. If not, it displays the usage and exits.

It then sets the input directory, output directory, and resolution based on the provided arguments. The script creates the output directory if it does not already exist.

Next, the script iterates over each file in the input directory. For each file, it checks if it is a regular file. If so, it extracts the filename and constructs the output file path.

Using the `convert` command (from ImageMagick), the script resizes the image to the specified resolution while maintaining the aspect ratio. It then displays a message indicating the successful resizing of the image.

Once all files in the input directory have been processed, the script outputs a message indicating the completion of the image resizing process.

This script provides a convenient way to resize multiple images in bulk to a specified resolution, making it useful for tasks like preparing images for a website or a presentation.