Wednesday, March 10, 2010

Automatically Resize Photos for Users

What it does
 
This is a script I wrote some time ago to automatically resize photos for some 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
 This same script is available at http://www.k12ltsp.org/mediawiki/index.php/Automatically_Resize_Photos_for_Users as it was originally posted there.

#!/bin/bash


# Copyright (C) 2006  Timothy Legge

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# Resize all the files in a directory when it runs.  The assumption is
# that this will be run via cron so the files dropped in the directory
# will be automatically resized at regular intervals
#

# 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

PROCESS_DIR=Desktop/resize_photos

# 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
        PHOTO_DIR=$HOME_DIR/$PROCESS_DIR
        RESIZE=$PHOTO_DIR/$NEW_SIZE             # 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" 3>/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

No comments: