User:Itu/scripts/Picrotate.sh

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
#  shellscript to rotate a pixelimage, using pnmrotate and subsequent cropping of the filling triangulars 
#  this is alpha-state and should be improved 
#  https://commons.wikimedia.org/wiki/User:Itu/scripts/Picrotate.sh     https://creativecommons.org/licenses/by-sa/3.0/


angle=$1
shift
sourcefile="$*"

if [[ -z "$*" ]]
then
        read -p "Filename: Pic to rotate: " sourcefile
	read -p "Angle to rotate $name (minus for clockwise) ?: "  angle
fi

echo  "Filename: Pic to rotate: " $sourcefile
echo  "Angle to rotate $name (minus is counterclockwise) : "  $angle

                   ###  if  // then wget 

filename=$(basename "$sourcefile")
ext0="${filename##*.}"
ext=$( echo $ext0  | tr 'A-Z' 'a-z'   )
echo "enforce lowercase > : " $ext
name="${filename%.*}"

echo filename: $filename
echo ext: $ext

ls -l $name*

echo "  rotate $name with an angle of $angle " 

convert -rotate "$angle"  "$sourcefile"   /tmp/rotatedpic.pnm  


dimension=$( identify /tmp/rotatedpic.pnm  | tr ' ' '\n' | grep x | head -1  ) 
w=$( echo $dimension | tr 'x' '\n' | head -1 )
h=$( echo $dimension | tr 'x' '\n' | tail -1 )

echo "Pic: Width: " $w "  Height: " $h

echo " calculate borders to be cropped (in lines of pixels) "

bw=$( echo "s($angle /180 *  3.141 ) * $h  " | bc -ql | tr -d \- | tr '.' '\n' | head -1  )
bh=$( echo "s($angle /180 *  3.141 ) * $w  " | bc -ql | tr -d \- | tr '.' '\n' | head -1  )
#^                            Pi        ^   

echo " Border in pixels: width: " $bw " height: " $bh

pnmcut -left $bw   -right $(( b - bw ))  -top $bh  -bottom $(( h - bh ))   /tmp/rotatedpic.pnm  > /tmp/rotatedpic_cropped.pnm

convert  /tmp/rotatedpic_cropped.pnm  "$name"_rot"$angle".$ext

echo display  "\"$name"_rot"$angle".$ext\"