Created
January 19, 2024 18:32
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filename | url | |
---|---|---|
jose-manuel-gulias-lugo.jpg | https://github.com/chepetime.png?size=500 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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
.Download Images: It downloads the images from the provided URLs into a directory named
./og
.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
.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:
curl
,jq
, and ImageMagick'sconvert
utility to be installed on macOS. Also, a valid TinyPNG API key is necessary for the compression step.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.