Advertisement
Guest User

Resize with imagemagick

a guest
Dec 7th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.93 KB | None | 0 0
  1. #!/bin/zsh
  2.  
  3. VERBOSE=0
  4. QUIET=0
  5.  
  6. if [ "x$1" = "x-q" ]
  7. then
  8.   shift
  9.   QUIET=1
  10. elif [ "x$1" = "x-v" ]
  11. then
  12.   shift
  13.   VERBOSE=1
  14. fi
  15.  
  16. sailoop=0
  17. arquivo="$1"
  18. raio="$2"
  19.  
  20. if [[ $# -eq 1 ]] # Only file as argument
  21. then
  22.   sailoop=0 # don't exit the loop
  23.   minsize=480000
  24.   maxsize=512000
  25. elif [[ $raio -lt 20.0 ]] # Two arguments, second is is the desired ratio
  26. then
  27.   sailoop=1 # exit the loop after changing the picture's size
  28. else
  29.   sailoop=0
  30.   minsize=$raio # the second parameter was not the ratio, but the minimum size
  31.   raio=""
  32.   if [[ "$3" == "" ]]
  33.   then
  34.     maxsize=512000
  35.   else
  36.     maxsize=$3
  37.   fi
  38. fi
  39.  
  40. desiredsize=$(((maxsize+minsize)/2)) # this number is integer
  41.  
  42. [ -f "$arquivo" ] || {
  43.   echo "**** Erro! Primeiro parametro \"$1\" precisa ser um arquivo legivel!" >&2
  44.   exit 1
  45. }
  46.  
  47. [[ $sailoop -eq 1 && "$raio" -lt 0.1 ]] && {
  48.   echo "**** Erro! Segundo parametro \"$2\" precisa ser o raio (numerico) de contracao!" >&2
  49.   exit 2
  50. }
  51.  
  52. destino="${arquivo//./-pequeno.}"
  53. largura=$(identify "$arquivo" | head -1 | sed 's/.* \([0-9].[0-9]*x[0-9].[0-9]*\) .*/\1/' | cut -f1 -dx)
  54. altura=$(identify "$arquivo" | head -1 | sed 's/.* \([0-9].[0-9]*x[0-9].[0-9]*\) .*/\1/' | cut -f2 -dx)
  55.  
  56. while [[ $sailoop -lt 2 ]]
  57. do
  58.   if [[ $sailoop == 1 ]]
  59.   then
  60.     sailoop=2 # raio was provided.
  61.   else
  62.     if [[ "$raio" == "" ]] # Initial raio
  63.     then
  64.       inputsize=$(/usr/bin/stat "$arquivo" | awk '($1=="Size:") {print $2}')
  65.       raio=$(echo -e "scale=3\nsqrt($inputsize/$desiredsize)" | bc -l) # rough estimate, square root of pictures ratio.
  66.     fi
  67.   fi
  68.  
  69.   novalargura=$((largura/raio))
  70.   novaaltura=$((altura/raio))
  71.   novalargura=${novalargura%%.*}
  72.   novaaltura=${novaaltura%.*}
  73.  
  74.   intermediario=$(/bin/mktemp)
  75.   /usr/bin/convert -resize ${novalargura}x${novaaltura}\! $arquivo "$intermediario"
  76.   /usr/bin/gifsicle --colors 256 -O2 "$intermediario" > "$destino"
  77.   rm -f "$intermediario"
  78.  
  79.   destsize=$(/usr/bin/stat "$destino" | awk '($1=="Size:") {print $2}')
  80.  
  81.   if [[ $destsize -gt $minsize && $destsize -lt $maxsize ]]
  82.   then
  83.     sailoop=2
  84.   elif [[ $destsize -ge $maxsize || $destsize -le $minsize ]]
  85.   then
  86.     proporcao=$(echo -e "scale=3\n($destsize/$desiredsize)" | bc -l) # let's fix up our ratio.
  87.     if [[ $VERBOSE -eq 1 ]]
  88.     then
  89.       echo "*** Not yet: size ${novalargura}x${novaaltura}, ratio $raio, target size $destsize"
  90.     fi
  91.     raio=$((((raio*proporcao)+raio)/2)) # we went bigger than expected, let's make the ratio bigger (this the target picture smaller)
  92.   else
  93.     echo "*** Internal error calculating sizes. Please check script. destsize=$destsize maxsize=$maxsize minsize=$minsize"
  94.     exit 1
  95.   fi
  96. done
  97.  
  98. if [[ $VERBOSE -eq 1 ]]
  99. then
  100.   echo "*** Final: size ${novalargura}x${novaaltura}, ratio $raio, target size $destsize"
  101. fi
  102. if [[ $QUIET == 1 ]]
  103. then
  104.   echo $(ls -lh "$destino" | awk '{print $5}')
  105. else
  106.   echo "*** $destino file size is $(ls -lh "$destino" | awk '{print $5}')."
  107. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement