Skip to content

Instantly share code, notes, and snippets.

@vidhav
Created August 26, 2015 13:31
Show Gist options
  • Save vidhav/e91449cf2eddfb9888eb to your computer and use it in GitHub Desktop.
Save vidhav/e91449cf2eddfb9888eb to your computer and use it in GitHub Desktop.
Fix image size and dpi recursively
#!/bin/bash
find . -type f -name '*.jpg' -print0 | while IFS= read -r -d '' file; do
report="$(date "+%Y-%m-%d %H:%M:%S")"
report+=" ($file)"
str="$(identify -format "%w;%h;%x;%y" "$file")"
arr=(${str//;/ })
w=${arr[0]:-0}
h=${arr[1]:-0}
dx=${arr[2]:-0}
dy=${arr[4]:-0}
d=0
if [ $dx == $dy ]; then
d=$dx
fi
if [ $d -gt 72 ]; then
convert="$(convert -units PixelsPerInch "$file" -density 72 "$file")"
report+=${convert:-\ \-converted}
fi
if [ $w -gt 1920 ]; then
resize="$(convert "$file" -resize 1920x "$file")"
report+=${resize:-\ \-resized}
fi
echo $report
echo $report >> ./images.log
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment