Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active August 29, 2015 14:13
Show Gist options
  • Save ik5/3ae3b92ea0febaf6b04e to your computer and use it in GitHub Desktop.
Save ik5/3ae3b92ea0febaf6b04e to your computer and use it in GitHub Desktop.
Optimize big jpg scans using imagemagic and jpgoptim programs
#/usr/bin/env bash
shopt -s nocasematch
function exec_code() {
file=$1
ext=${file: -4}
if [[ $ext == '.jpg' ]]; then
echo ', a jpeg file, working on it'
mv "$file" "old_$file"
convert "old_$file" -resize '40%' "$file"
jpegoptim -s --all-progressive -f "$file"
rm "old_$file"
else
echo ', not a jpeg file, ignoring'
fi
}
function ls_dir() {
dir=$1
cd "$dir"
for file in * ; do
echo -n "Testing: $file"
if [[ -d "$file" ]]; then
echo -n ' it is a directory'
if [[ "$file" != '.' && "$file" != '..' ]]; then
echo -n ' and we are going inside'
ls_dir "$file"
cd ..
fi
else
echo -n ' normal file, inspecting'
exec_code "$file"
fi
echo -n "\n"
done
}
# no argument for a directory to use
if [ -z "$1" -a "$1" == "" ]; then
ls_dir './'
else
if [ -d "$1" ]; then
ls_dir "$1"
else
echo "Invalid argument was provided: $1"
fi
fi
shopt -u nocasematch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment