Not sure if you consider this an improvement – oh well...
Code:
#!/bin/bash
MAXsize=1900000;
while read v; do
h=`sips -g pixelHeight "$v" | sed -n '${s/.* //;p}'`
w=`sips -g pixelWidth "$v" | sed -n '${s/.* //;p}'`
pxlnmbr=$(( $h * $w));
echo "Number of Pixels in $v ($w x $h) is $pxlnmbr";
if [[ $pxlnmbr -gt $MAXsize ]]; then
factor=`echo "sqrt($MAXsize *100^2/ $pxlnmbr) " | bc`;
echo "factor: $factor%";
newWidth=$(( $w * $factor / 100 ))
newHeight=$(( $w * $factor / 100 ))
echo "$v new: $newWidth x $newHeight";
sips -z $newHeight $newWidth "$v"
echo " ";
fi
done < <(find $1/ -name "*.jpeg" -or -name "*.jpg" -or -name "*.gif" -or -name "*.png")
($anzahl should be replaced by $pxlnumber,
nicht so?)
By using a while loop instead of a for loop, file names with spaces are permitted (can't remember if they're permitted according to ePub specs, though), and enclose $v in "".
Sticking the find clause at the end is for a very good reason, but a very complicated technical one that I cannot recall just at the moment...
None of the rest can strictly be called improvements, rather just a demonstration of "there's more than one way to do it", and my own preferences (or prejudices?) for making scripts compact (or do I mean unreadable?)
For simple integer arithmetic, this can be done directly in the shell within
$(( ... )) parenthesis, I therefore express the resizing factor as a %; two-digit accuracy should be sufficient for the intended purpose.
Don't have a fruit machine, so I haven't been able to test it properly.