View Single Post
Old 06-14-2013, 02:01 PM   #11
SBT
Fanatic
SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.SBT ought to be getting tired of karma fortunes by now.
 
SBT's Avatar
 
Posts: 580
Karma: 810184
Join Date: Sep 2010
Location: Norway
Device: prs-t1, tablet, Nook Simple, assorted kindles, iPad
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.
SBT is offline   Reply With Quote