I have this script that is to rescale images to a percentage value
#!/bin/bash
percent=$1
echo $percent
for img in `find *.png`;
do
echo Processing file $img
width=$( mdls $img | grep kMDItemPixelWidth | tail -n1 | cut -d= -f2 )
height=$( mdls $img | grep kMDItemPixelHeight | tail -n1 | cut -d= -f2 )
newWidth=$((width*percent))
newHeight=$((height*percent))
echo $newWidth $newHeight
sips -z $newWidth $newHeight $img
done
My bash is configured to accept commas as decimal separators.
So, whey I type
rescale 0,3019
I am trying to rescale the images to 30.19% of their values
the problem is that the line
echo $newWidth $newHeight
shows me the values as they were multiplied by 3019. Strangely the first echo
echo $percent
shows me 0,3019 (the correct value)
what am I missing?
awk
is available on nearly any system that hasbash grep cut
and can do the text selections and floating-point in one command:mdls $img | awk -F= -vf=$img -vp=,33 '/kMDItemPixelHeight{h=$2*p}/kMDItemPixelWidth{w=$2*p} END{system("sips -z " w " " h " " f)}'
(plus minor tweaks for decimal-comma if your locale doesn't handle it, or if $img contains special chars). – dave_thompson_085 Oct 04 '14 at 07:24