Skip to content

Instantly share code, notes, and snippets.

@chepetime
Created January 19, 2024 18:32
Show Gist options
  • Save chepetime/337e7bc2737d41fcfb2b758762bd5f45 to your computer and use it in GitHub Desktop.
Save chepetime/337e7bc2737d41fcfb2b758762bd5f45 to your computer and use it in GitHub Desktop.
Shell Script for macOS: Download, Resize, and Compress Images from a CSV File using TinyPNG and ImageMagick
#!/bin/zsh
# Define the CSV file path. The CSV should contain two columns: filename and URL.
csvFile="images.csv"
# Provide your Tinify API Key. Replace with your actual key.
apiKey="#############################"
# Create directories for original, resized, and compressed images.
mkdir -p ./og
mkdir -p ./resized
mkdir -p ./resized-compressed
echo "Starting processing of images..."
# Calculate the total number of images listed in the CSV, excluding the header.
totalLines=$(($(wc -l < "$csvFile") - 1))
currentLine=0
# Read from CSV, download and resize images.
echo "Downloading and resizing images..."
tail -n +2 "$csvFile" | while IFS=, read -r filename url; do
((currentLine++))
echo "Processing image $currentLine of $totalLines: $filename"
# Download each image into the ./og directory.
curl -s -o "./og/$filename" "$url"
# Resize the image to 40% of its original size and save it in the ./resized directory.
convert "./og/$filename" -resize 40% "./resized/$filename"
done
echo "Downloading and resizing completed."
# Compress all resized images and move them to the ./resized-compressed directory.
echo "Compressing images..."
currentFile=0
for file in ./resized/*; do
((currentFile++))
filename=$(basename "$file")
echo "Compressing image $currentFile of $totalLines: $filename"
# Use the Tinify API to compress each image.
jsonResponse=$(curl --silent --user api:"$apiKey" \
--data-binary @"$file" https://api.tinify.com/shrink)
# Extract the URL of the compressed image from the API response.
compressedUrl=$(echo $jsonResponse | jq -r '.output.url')
# Download the compressed image and save it in the ./resized-compressed directory.
curl --silent "$compressedUrl" --output "./resized-compressed/$filename"
done
echo "Compression completed."
echo "All processes completed successfully."
filename url
jose-manuel-gulias-lugo.jpg https://github.com/chepetime.png?size=500
brew update
brew upgrade
brew cleanup -s
brew doctor
brew missing
# Install script dependencies
brew install imagemagick jq
# Get TinyPNG API Key
open https://tinypng.com/developers
@chepetime
Copy link
Author

Shell Script for macOS: Download, Resize, and Compress Images from a CSV File using TinyPNG and ImageMagick

Purpose:
This shell script is designed for macOS users. It automates the process of downloading images from URLs listed in a CSV file, resizing them, and then compressing them using TinyPNG's API and ImageMagick. This script is ideal for batch processing a large number of images for web optimization or storage efficiency.

Functionalities:

  1. Reads URLs from a CSV File: The script starts by reading a CSV file where each line contains a filename and a URL, separated by a comma. The format of the CSV should be filename,url.

  2. Download Images: It downloads the images from the provided URLs into a directory named ./og.

  3. Resize Images: Utilizing ImageMagick's convert command, the script resizes each downloaded image to 40% of its original size and saves them in a separate directory ./resized.

  4. Compress Images: In the final step, the script compresses the resized images using the TinyPNG API. The compressed images are then stored in a third directory, ./resized-compressed.

Technical Details:

  • Pre-requisites: The script requires curl, jq, and ImageMagick's convert utility to be installed on macOS. Also, a valid TinyPNG API key is necessary for the compression step.
  • Progress Indicators: Throughout its execution, the script provides informative progress messages. It indicates the start and completion of downloading, resizing, and compression steps, along with progress updates for each image processed.
  • Modular Structure: The script is structured in a way that segregates each major step (downloading, resizing, and compressing) into distinct phases. This modular approach makes it easy to understand and maintain.
  • Error Handling: While the script includes basic progress indicators, users should be aware that error handling (e.g., for invalid URLs, API failures) is minimal and might need enhancement for more robust use cases.

Usage:

To use the script, place the CSV file in a known directory, update the script with the CSV file's path, and ensure that the API key for TinyPNG is correctly set. Running the script will process all images listed in the CSV file in batches and output them in the specified directories.

This script is a practical tool for web developers, graphic designers, and content managers who frequently work with large sets of images and require an automated process for optimizing them efficiently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment