Bash script to resize images recursively and place them i a new directory

Running out off disk space on you mobile device like Tabled, Phone of laptop and because of the big file size of your photo files? But you do not want to delete your photos? Resize them to small files.

First: copy all your original photos to a backup medium like a Desktop PC, Server or a NAS. Rename the photo directory to Pictures. So that the pictures are in /home/rob/MyDocs/Pictures . You may have as many subdirectories as you want with directories names you made yourself.

The structure looks like this:

  • /home/rob/MyDocs/Pictures/album_1
  • /home/rob/MyDocs/Pictures/album_2
  • /home/rob/MyDocs/Pictures/holliday/2012
  • /home/rob/MyDocs/Pictures/holliday/2013
  • /home/rob/MyDocs/Pictures/mydog/2010
  • /home/rob/MyDocs/Pictures/mydog/2012
  • and so on.....

Place the scrip in ./Pictures and run is with:

bash resize.sh

Note: not sh resize.sh
Or give file execution permission whith and run it:

chmod 755 resize.sh
./resize.sh

The script build the same directory structure besides the ./Picture/ dir with the name ./Picture_Small/ with the same subdirs. The photo files are resize to 1024 px.

#!/bin/bash

# By Rob Groen 0ctober 2014 info@robgroen.nl
#
# Resize pictures (*.jpg|JPG) and put them in a new create directory (if not excist)
# Script needs to be executed in de dir above ./Picture if the
# picturers ar in /home/rob/MyDocs/Pictures place the script in
# like /myDocs an run bash resize.sh   (**** Use bash NOT sh *****)


OIFS="$IFS"
IFS=$'\n'
COUNT=1
for IN in `find ./Pictures -iname "*.jpg" -o -iname "*.png"`
do
OUT=${IN/Pictures/Pictures_Small}
DIR=$(dirname $OUT)
mkdir -p "$DIR"
if [ ! -f $OUT ]; then
echo "$COUNT Converting $IN"
convert $IN -resize 1024  $OUT
else
echo "$COUNT Skip $IN converted al ready."
fi

COUNT=$[COUNT + 1]
done
IFS="$OIFS"

This script resize images recursively

Reply
The content of this field is kept private and will not be shown publicly.
CAPTCHA
Deze vraag is om te controleren of u een persoon bent. Di is om spam berichten tegen te gaan.