Skip to content

Instantly share code, notes, and snippets.

@4aficiona2
Created July 28, 2016 16:05
Show Gist options
  • Save 4aficiona2/62e842d40ee2171ea45bf59f8fadf0af to your computer and use it in GitHub Desktop.
Save 4aficiona2/62e842d40ee2171ea45bf59f8fadf0af to your computer and use it in GitHub Desktop.
Resample specified images to 72 DPI
#!/bin/bash
if [[ ! "$1" || "$1" == "-h" || "$1" == "--help" ]]; then cat <<HELP
adapted from https://gist.github.com/rraallvv/d5336521a9dbe97123c6
Resample specified images to 72 DPI
http://benalman.com/
Usage: $(basename "$0") [img [img ...]]
The new MacBook Pro retina display is amazing, but screengrabs taken on
one using the screencapture utility aren't scaled to 72 DPI by default.
This script scales those images to 72 DPI, making them viewable at a sane
resolution in web browsers.
Copyright (c) 2012 "Cowboy" Ben Alman
Licensed under the MIT license.
http://benalman.com/about/license/
HELP
[[ "$1" ]]; exit; fi
while [[ "$1" ]]; do
file="$1"; shift
dpiWidth=$(sips "$file" -g dpiWidth | awk '/:/ {print $2}')
dpiHeight=$(sips "$file" -g dpiHeight | awk '/:/ {print $2}')
pixelWidth=$(sips "$file" -g pixelWidth | awk '/:/ {print $2}')
pixelHeight=$(sips "$file" -g pixelHeight | awk '/:/ {print $2}')
if [[ "$(echo "$dpiWidth - 72" | bc)" == "0" || "$(echo "$dpiHeight - 72" | bc)" == "0" ]]; then
echo "File $(basename "$file") already ${pixelWidth}x${pixelHeight} pixels @ 72 DPI."
continue
fi
#w=$(echo "$pixelWidth * 72 / $dpiWidth" | bc)
#h=$(echo "$pixelHeight * 72 / $dpiHeight" | bc)
#echo "Resampling $(basename "$file") to ${w}x${h} pixels @ 72 DPI."
#sips "$file" -s dpiWidth 72 -s dpiHeight 72 -z $h $w >/dev/null 2>&1
# preserves original width and height and only outputs with 72dpi
echo "Resampling $(basename "$file") to ${pixelWidth}x${pixelHeight} pixels @ 72 DPI."
sips "$file" -s dpiWidth 72 -s dpiHeight 72 -z $pixelHeight $pixelWidth >/dev/null 2>&1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment