Reescalar imagenes por consola

Bueno a veces nos encontramos gente en la empresa que le gusta poner fotos en los directorios de red por distintos motivos. El problema es que no son conscientes de que una foto copiada directamente de una camara suele tener una resolucion bastante grande y cada foto, si tienes suerte y esta en jpg), suele pesar unos 2-3 Mb.

Lo malo es cuando un dia ves que las copias de seguridad no caben en los dispositivos de almacenaje y te das cuenta que es por culpa de 800 trillones de fotos "chorras" que ocupan 3 MB cada una.

Solución: passarlas a 800x600 que ocupan unos miseros 100-200kb y se ven igual.

Pero... ¿como no hacerlo una por una?
Respuesta:

- mogrify (imagemagick) + identify
- find

PASO 1

creamos un script tal que así:
#!/bin/bash
###################################

INPUT="$1"

#tamaño actual de la imagen
width=$(identify -format %w "$INPUT")
heigh=$(identify -format %h "$INPUT")

#una relacion de aspecto
relacio=$(echo "scale=4;$width/$heigh" | bc)

#tamaño nuevo: 800 de ancho y una altura con la misma relacion
neww=800
newh=$(echo "$neww/$relacio" |bc)

echo "$1: '$width'x'$heigh' -> '$neww'x'$newh'"
mogrify -resize "$neww"x"$newh" "$1"

PASO 2

utilizamos la herramienta find:

find . -iname "*.jpg" -size +1024k -exec /path_al_script/img800.sh '{}' \;


SALIDA

./INVENTARIOS/FOTOS VAJILLA/DSC02546.JPG: '2048'x'1536' -> '800'x'600'
./INVENTARIOS/FOTOS VAJILLA/DSC02548.JPG: '2048'x'1536' -> '800'x'600'
./INVENTARIOS/FOTOS VAJILLA/DSC02549.JPG: '2048'x'1536' -> '800'x'600'
./INVENTARIOS/FOTOS VAJILLA/DSC02550.JPG: '2048'x'1536' -> '800'x'600'
./INVENTARIOS/FOTOS VAJILLA/DSC02551.JPG: '2048'x'1536' -> '800'x'600'
./INVENTARIOS/FOTOS VAJILLA/DSC02553.JPG: '2048'x'1536' -> '800'x'600'
./INVENTARIOS/FOTOS VAJILLA/DSC02554.JPG: '2048'x'1536' -> '800'x'600'

OPCION 2

También se puede programar un service menu para kde4 de manera que tengas una acción para los plasmas o dolphin. El script te pide si quieres poner un ancho a mano o si quieres dividir sus dimensiones por 2,4,6,8... y te crea una imagen con la terminación .xxx

#!/bin/bash

SCRIPT=/home/beavies/run.sh
rm -f $SCRIPT

OPCIO=$(kdialog --menu "Estil pel resize:" A "Dividir" B "Manual")

if [ "$?" = 0 ]; then
  
  if [ "$OPCIO" = A ]; then
     
     DIVISOR=$(kdialog --menu "Dividir per:" 2 "/2" 4 "/4" 6 "/6" 8 "/8")     
     if [ $? = 1 ]; then
        exit
     fi;
  
  elif [ "$OPCIO" = B ]; then
     neww=$(kdialog --title "WIDTH" --inputbox "Nuevo width de la imagen" "1024")
  fi;

else
  exit 0
fi;

while (( $# ))
do
  INFILE=$1
  OUTFILE="$INFILE".xxx
  shift
  
  #tamaño actual de la imagen
  width=$(identify -format %w "$INFILE")
  heigh=$(identify -format %h "$INFILE")

  #una relacion de aspecto
  if [ "$OPCIO" = B ]; then
    relacio=$(echo "scale=4;$width/$heigh" | bc)
    newh=$(echo "$neww/$relacio" |bc)
  elif [ "$OPCIO" = A ]; then
    neww=$(echo "$width/$DIVISOR" | bc)
    newh=$(echo "$heigh/$DIVISOR" | bc)
    
  fi;

  echo "echo \"$INFILE: '$width'x'$heigh' -> '$neww'x'$newh'\"" >> $SCRIPT
  echo "convert -resize '$neww'x'$newh' \"$INFILE\" \"$OUTFILE\"" >> $SCRIPT

done

chmod +x $SCRIPT
konsole -e /bin/sh -c $SCRIPT


Comentarios