Automatically Resize Photos for Users

From K12LTSP Wiki

Jump to: navigation, search

Contents

What it does

This is a script I wrote to automatically resize photos for my users. With this script, they can simply copy their digital camera photos to a directory in their home directory. When the script runs via cron it will use ImageMagick's convert utility to shrink the photo to the default size (1024x768) with a quality of 75 and delete the original.

It will automatically create the required directories if they do not exist

Why

Some users are not comfortable with using ImageMagick and few of the available gui tools are as fast at resizing multiple files. With this script, the users simply drop the files in place, wait a few minutes and the cron job resizes the files for them.

It also has the added benefit of reducing the storage requirements for photos

Potential issues

The script automatically deletes the original. This is by design don't complain to me if you lose original work.

ToDo

The script currently meets my needs but I would probably add the ability for the user to specify a default size, quality, photo directory and the option of keeping the originals via a config file in their home directory

Script

<verbatim>

  1. ====/bin/bash ====
  1. Copyright (C) 2006 Timothy Legge
  1. This program is free software; you can redistribute it and/or
  2. modify it under the terms of the GNU General Public License
  3. as published by the Free Software Foundation; either version 2
  4. of the License, or (at your option) any later version.
  1. Resize all the files in a directory when it runs. The assumption is
  2. that this will be run via cron so the files dropped in the directory
  3. will be automatically resized at regular intervals
  1. Checking for root privelages to call script:

ROOT_UID=0 # Root has $UID 0. if "$UID" -eq "$ROOT_UID" # Check to ensure user is root.. then

PASSWORD_FILE=/etc/passwd NEW_SIZE=1024x768 # New Size QUALITY=75 # Default jpg quality

PROCESSDIR=Desktop/resizephotos

  1. Exclude the following directories

exclude_dirs="/home/ltcc3

       /home/ltcc4
       /home/al

/home/don"

echo "This script will process photos in all users ~/photos directory and" echo "DELETE all the original photos"; echo echo "YOU HAVE BEEN WARNED"; echo echo "Comment the following line if you know what you are doing"

exit 0 # Comment or delete line to use script

for name in $(awk 'BEGIN{FS":"}{if ($3>500) print $1;}' < "$PASSWORD_FILE" ) do

       HOME_DIR=/home/$name
       if  ==== -e "$HOME_DIR" 
====
       then
               continue        # User does not have a home directory
       fi
       PHOTODIR=$HOMEDIR/$PROCESS_DIR
       RESIZE=$PHOTODIR/$NEWSIZE             # Resize to most monitor resolitions
       skip_dir=0
       # Check to see whether the current directory is
       # in the list of excluded directories
       for exclude in $exclude_dirs
       do
               if [[[ "$exclude"  "$HOME_DIR" ]]]
               then
               #       echo "Exclude : $exclude"
                       skip_dir=1
                       break   # Directory is excluded break out of the loop
               fi
       done
       # Check to see whether the directory should be skipped (in excluded list)
       if [[[ "$skip_dir"  "1" ]]]
       then
               continue        # Exclude directory go to next directory
       else    # Process the photos in the directories as normal
               # Check to see whether the $PHOTO_DIR exists
               # Create it if it does not
               if  ==== -e "$PHOTO_DIR" 
====
               then
                       echo "Photo resize directory does not exist"
                       echo "    Creating $PHOTO_DIR"
                       if mkdir "$PHOTO_DIR" 2>/dev/null;
                       then
                               chown $name:$name $PHOTO_DIR
                               echo "        $PHOTO_DIR successfully created"
                               # Create the $RESIZE_DIR since its parent did not exist
                               if mkdir "$RESIZE" 2>/dev/null
                               then
                                       chown $name:$name $RESIZE
                                       echo "        $RESIZE successfully created"
                               else
                                       echo "        ERROR: unable to create $RESIZE"
                                       continue
                               fi
                       else
                               echo "    ERROR: unable to create $PHOTO_DIR"
                               continue
                       fi
                       continue
               fi
               # Check to see whether the location for the resized files exists
               # Create it if it does not
               if  ==== -e "$RESIZE" 
====
               then
                       echo "Location for resized photos does not exist"
                       echo "    Creating $RESIZE"
                       if mkdir "$RESIZE" 2>/dev/null;
                       then
                               chown $name:$name $RESIZE
                               echo "        $RESIZE successfully created"
                       else
                               echo "        ERROR: unable to create $RESIZE"
                               continue;
                       fi
               fi
               # Start Processing files
               echo "Begin processing pictures in $PHOTO_DIR"
               # Loop through each file in the $PHOTO_DIR
               for file in $PHOTO_DIR/*
               do
                       if  ==== -e "$file"   # Check to see if any files exist
====
                       then
                               echo "$file does not exist."
                               continue
                       fi
                       # Only process regular file types (not directories)
                       if  -f "$file" 
                       then
                               echo "    convert "$file" -resize $NEW_SIZE -quality 75 $RESIZE/`basename "$file"`"
                               if convert "$file" -resize $NEW_SIZE -quality 75 "$RESIZE/`basename "$file"`" 2>/dev/null
                               then
                                       if rm -f "$file" 2>/dev/null
                                       then
                                               echo "        Successfully deleted $file"
                                       else
                                               echo "        Error attempting to delete $file"
                                       fi
                               else
                                       echo "        Error attempting to resize $file"
                                       echo "            Original file not deleted"
                               fi
                       fi
               done
               chown $name:$name $RESIZE/*
               # Finish Processing files
               echo "No resizable pictures remaining in $PHOTO_DIR"
       fi      # Directory not excluded - Process photos

done else # not root

       echo "Script must be run as root"

fi exit 0

</verbatim>

Personal tools