Skip to content

Instantly share code, notes, and snippets.

@tryone144
Created December 30, 2015 17:29
Show Gist options
  • Save tryone144/61c236c4cb5e0276e6ec to your computer and use it in GitHub Desktop.
Save tryone144/61c236c4cb5e0276e6ec to your computer and use it in GitHub Desktop.
Resize images (shrink only) to screen sized .jpg
#!/bin/bash
#
# Resize images (shrink only) to screen sized .jpg
#
# (c) 2015 Bernd Busse
#
BLUE="\e[1;34m"
RED="\e[1;31m"
GREEN="\e[1;32m"
RESET="\e[0m"
echo " === Image Resize and Convert === "
folders=
f_len=0
f_ctr=0
_PWD="$( pwd )"
# filter arguments
while [[ -n ${1} ]]; do
if [[ -d "${1}" ]]; then
folders="${folders}${folders:+\n}${1}"
fi
shift
done
f_len=$( echo -e "${folders}" | wc -l )
# go to given directories
while read -r d; do
f_ctr=$(( ${f_ctr} + 1 ))
echo -e "${BLUE}::${GREEN} entering \"${d}\" (${f_ctr}/${f_len})${RESET}"
cd "${d}"
if (( ${?} != 0 )); then
exit 10
fi
ctr=0
for f in ./*; do
ctr=$(( ${ctr} + 1 ))
info="$( identify -format '%w:%h:%e' "${f}" 2> /dev/null)"
if (( ${?} != 0 )); then
# not a compatible image file
continue
fi
width=$( echo "${info}" | cut -d ':' -f 1 )
height=$( echo "${info}" | cut -d ':' -f 2 )
ftype="$( echo "${info}" | cut -d ':' -f 3 )"
if (( ${width} > 1920 )) || (( ${height} > 1200 )) || [[ ${ftype} != "jpg" ]]; then
# convert image
echo -e " ${BLUE}::${GREEN} converting \"${f}\"${RESET}"
convert "${f}" -resize 1920x1200\> "${f%.*}.jpg"
if [[ ${ftype} != "jpg" ]]; then
# delete old file
echo -e " ${BLUE}::${RED} delete \"${f}\"${RESET}"
rm "${f}"
fi
fi
# progress output
if (( (${ctr} % 100) == 0 )); then
echo -e " ${BLUE}[PROGRESS]${GREEN} Checked ${ctr} files${RESET}"
fi
done
echo -e "${BLUE}::${GREEN} leaving \"${d}\"${RESET}"
cd "${_PWD}"
done < <( echo -e "${folders}" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment