Skip to content

Instantly share code, notes, and snippets.

@spokanemac
Last active May 11, 2023 18:16
Show Gist options
  • Save spokanemac/0a11b95dd082e1b2fd9750b212dd8627 to your computer and use it in GitHub Desktop.
Save spokanemac/0a11b95dd082e1b2fd9750b212dd8627 to your computer and use it in GitHub Desktop.
Script loops over image filename(s) passes as an argument, upscaling it to 144 dpi using sips. It uses sips again to get the new width and height of the image, and renames the file to include the width, height, and "@2x" in the filename.
#!/bin/zsh
# Plugin: Scale and Rename Image
# Description: upscales an image to 144 dpi and renames the file to append the dimmensions.
# Author: Jack-Daniyel Strong <[email protected]>
# Date: 11-May-2023
# Version: 0.1
# ==============================================================================
if [ $# -eq 0 ]; then
echo "No arguments provided. Please provide image file(s) as arguments."
exit 1
fi
# Loop through image(s) passed
for image in "$@"; do
# Check if file exists and has a known file extension
if [[ -f $image && $image =~ \.(jpg|jpeg|png|gif)$ ]]; then
# Upscale the image to 144 dpi
/usr/bin/sips -s dpiHeight 144.0 -s dpiWidth 144.0 $image
# Get the new width and height
width=$(/usr/bin/sips -g pixelWidth $image | tail -n 1 | cut -d" " -f4)
height=$(/usr/bin/sips -g pixelHeight $image | tail -n 1 | cut -d" " -f4)
# Extract directory, base name and extension
directory=$(dirname "$image")
base_name=$(basename "$image" | cut -d. -f1)
extension="${image##*.}"
# Rename the file
mv "$image" "${directory}/${base_name}-${width}x${height}@2x.$extension"
echo "Processed $image"
else
echo "File $image not found."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment