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

38 comments:

俊達 said...

thx u very much, i learn a lot

雅琳雅琳 said...

how do u do?................................................................

林淑凡 said...

愛,拆開來是心和受兩個字。用心去接受對方的一切,用心去愛對方的所有。......................................................

火芬 said...

人必須心懷希望,才會活的快樂,日子才過得充實,有意義,有朝氣,有信心。........................................

PorshaCoghlan梁子珠 said...

I love readding, and thanks for your artical. ........................................

RooseveltHouchi嬌琪 said...

很喜歡你的文章~繼續加油唷^^. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

瑋玲 said...

how do u do?................................................................

尚舒 said...

I love readding, and thanks for your artical..............................................

婷妏 said...

噴泉的高度,不會超過它的源頭。一個人的事業也是如此,它的成就絕不會超過自己的信念。.............................................

Avri221lE_Pryce1 said...

君子立恆志,小人恆立志。.............................................

怡君 said...

Hello,這裡真是百看不厭的部落格........................................

怡谷ShanikaMaxey怡谷 said...

本土性愛自拍圖台灣本土性愛性愛a成人性愛站明星性愛成人dvd下載成人dvd免費看成人go成人go2成人hbo成人kk俱樂部成人人圖成人大全成人片a片成人片gogo成人片gogosex成人卡通色情片白虎圖庫尼可成人網弘爺論壇打非打非機專用網打炮論壇打飛機專區打砲遊戲打砲網站本土付費av本土成人直播本土自拍小弟弟本土性自拍影片85cc聊天室aio交友愛情館成人視訊

文君 said...

Many a little makes a mickle.............................................................

estherme said...

Make hay while the sun shines.........................................

耿賢耿賢 said...

朝著既定的目標走,就不會迷失。......................................................................

曉薇 said...

要持續更新下去喲!!祝你心情愉快.............................................................

江婷 said...

生存乃是不斷地在內心與靈魂交戰;寫作是坐著審判自己。.................................................................                           

家賢 said...

人不能像動物一樣活著,而應該追求知識和美德....................................................................

堅強堅強 said...

向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

佳皓佳皓 said...

人有兩眼一舌,是為了觀察倍於說話的緣故。............................................................

懿綺懿綺 said...

Thanks a lot for sharing (o>▽<)..................................................................

chen said...

海鷗要高飛,必先遠退。花蜜要香醇,必先久釀。............................................................

婷珊 said...

加油-不論如何都支持你............................................................

淑蔡承均dgd周真淑ii淑 said...

與其期盼別人疼你,不如自己疼自己。..................................................

陳湘枝陳湘枝 said...

Gods mill grinds slow but sure...................................................

楊儀卉 said...

良好的開端,已是成功的一半。..................................................

王美妹 said...

blog有留言互動才好玩~~希望留言能支持您的更新動力!!!............................................................

姿柯瑩柯dgdd憶曾g智曾 said...

看得見您的用心~~希望這裡愈來愈熱鬧哦~~............................................................

鄭美玉 said...

做好事,不需要給人知道,雖然只是一件微不足道的事,但我相信,這會帶給我快樂。..................................................

怡謝伶 said...

人生中最好的禮物就是屬於自己的一部份............................................................

又瑞許又瑞許又瑞許 said...

獨居時,要反省自己的過錯;在社會大眾之間,則要忘卻別人的過失。..................................................

宣麟至上 said...

愛情不是慈善事業,不能隨便施捨。......................................................................

于庭吳 said...

看著你的BLOG 好多朋友都回應 真厲害..................................................

陳李原筠永辛 said...

你好勁呀! 感謝!!!............................................................

1615 said...

想跟你說一聲加油,祝福大家每天開心............................................................

璇陳陳陳竹 said...

快樂,是享受工作過程的結果......................................................................

洪勳劉耀德劉耀德華 said...

你的分享很不錯.. 謝謝 ..................................................

孫邦柔 said...

辛苦了!祝你愈來愈好!............................................................