change to recursive mode is easy, i'll change it. i assume the output dir structure should reflect the structure of the source?
Code:
#!/bin/bash
shopt -s globstar
src=$HOME/Obrazy
dest="$HOME/resized_files"
limit=480000
mkdir -p "$dest"
for f in "$src"/**/*{jpg,png}
do
dim=$( identify "$f" | grep -Pow '[0-9]+x[0-9]+' | head -1 )
w=${dim%x*}
h=${dim#*x}
if (( w*h>limit ))
then
echo "* resizing '$f'"
echo "width:$w, height:$h, pixel count:$((w*h))"
scale=$( echo "sqrt( $limit/($w*$h))*100" | bc -l )
scale=${scale:0:6}%
echo scale:$scale
rf=$dest/${f#$src/}
convert -scale "$scale" "$f" "$rf"
rdim=$( identify "$rf" | grep -Pow '[0-9]+x[0-9]+' | head -1 )
rw=${rdim%x*}
rh=${rdim#*x}
echo "* resized file '$rf'"
echo "new width:$rw, new height:$rh, pixel count:$((rw*rh))"
echo
fi
done
globstar ** is a bashism that matches 0 or more directories
alternative would be to use find + while read
Code:
while read -rd $'\0' f
do
...
done < <( find "$src" ... -print0 )
in case you have images with capitalized extensions (digital cameras often do that) you can use shopt -s nocaseglob so you don't have to explicitly list capitalized forms *{jpg,JPG,png,PNG} and all possible variations Jpg Png etc. or write the globs this way:
Code:
for f in "$src"/**/*.[jJ][pP][gG] "$src"/**/*.[pP][nN][gG]
Bookmarks