logo
hero
Štefan Hosťovecký

Web Consultant & Specialist & Full Stack Developer

Automating Image Conversion and Resizing Using a Bash Script

hero

Intro

In the ever-evolving digital landscape, dealing with administrative tasks often lands us on government websites, submitting forms and uploading document scans. Such was my situation, when I found myself having to upload 5 to 10 scanned documents for a car-related form submission. But here's the catch - the total size had to be less than 5MB.

This could have been a moment of stress, as the size of high-quality scans and the added complexity of dealing with HEIC files (thanks to my iPhone) could have turned this simple task into a chore. But as a coder, I view these instances as opportunities, not obstacles. Opportunities to leverage my programming skills to streamline and automate the process, making the future more efficient.

So, I took this challenge head-on and created a bash script to automate the process of converting these HEIC images to JPEG and resizing them, all while ensuring the final size remains under the specified limit.

#!/bin/bash

input_folder="/path/to/input/folder"
output_folder="/path/to/output/folder"

# Create the output folder if it doesn't exist
mkdir -p "$output_folder"

# Iterate over all files in the input folder
for filename in "$input_folder"/*; do
    # Verify if the file is an image
    if [[ -f "$filename" && "$filename" =~ \.(heic|HEIC|heif)$ ]]; then
        # Strip the filename and extension
        file_basename=$(basename "$filename")
        file_extension="${file_basename##*.}"

        # Exclude the extension from the filename
        file_name="${file_basename%.*}"

        # Formulate the output filename
        output_name="$output_folder/$file_name.jpg"

        # Convert the image format and resize
        sips -s format jpeg -Z "$(sips -g pixelHeight -g pixelWidth "$filename" | awk '/pixel/{printf "%dx%d\n", $2/2, $4/2}')" "$filename" --out "$output_name"

        echo "Converted $filename to $output_name"
    fi
done

echo "Conversion complete!"

This bash script acts on all images in a designated input folder, converting them from HEIC/HEIF to JPEG format, resizing them to half their original size, and saving them in a specified output folder.

Running the Script

  • Substitute /path/to/input/folder with the real path to your input folder, and /path/to/output/folder with the path where you want the output to go.
  • Save the script to a file. Let's say heic.sh.
  • Grant the file execution permissions with chmod +x heic.sh.
  • Execute the script with ./heic.sh.

The script will traverse all files in the input directory, examine if they're .heic or .heif images, convert them to JPEG, resize them to 50% of their original size, and save them to the defined output directory. The new filenames will have a .jpg extension.

It will also echo the conversion progress and will notify you when the task is complete. You can then find the converted, resized images in your specified output folder.

Final Thoughts

This bash script offers a pragmatic way to automate the cumbersome task of image conversion and resizing. Bash scripts like this one are tools every programmer should be comfortable with, as they can turn repetitive tasks into one-liners. If you wish to dig deeper into bash scripting, consider checking out the GNU Bash Manual. Scripts like this are just the tip of the iceberg when it comes to what's possible. The more you dive into scripting, the more time you can save on mundane tasks, freeing up your time for the more interesting problems. Happy coding!

You can find many of interesting resources here: https://stackoverflow.com/questions/tagged/bash