Skip to content

Instantly share code, notes, and snippets.

@rraallvv
Created January 27, 2016 12:34
Show Gist options
  • Save rraallvv/d5336521a9dbe97123c6 to your computer and use it in GitHub Desktop.
Save rraallvv/d5336521a9dbe97123c6 to your computer and use it in GitHub Desktop.
Resample images to 72 DPI using sips
#!/bin/bash
if [[ ! "$1" || "$1" == "-h" || "$1" == "--help" ]]; then cat <<HELP
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
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment