#!/bin/csh -f # makethumbs # ------------------------------------------------------------------------- # C shell script to create a "thumbs" folder of thumbnails of all images # (JPG, GIF, PNG, etc.) in the current directory. # ------------------------------------------------------------------------- # Revision History: # $Log$ # ------------------------------------------------------------------------- # Collect command line options set option_interactive_string = "-i" while ($#argv > 0) if ("$1" == "-h" || "$1" == "--help") then echo "Usage: $0:t [options]" echo "Options:" echo " -h = Show this help text" echo " --help = Show this help text" echo " -q = Quick. Skip prompt when deleting obsolete files." exit 1 else if ("$1" == "-q") then shift set option_interactive_string = "" else if ("-" == "`echo $1:q | cut -c 1`") then echo "Error: Invalid option: $1:q" $0 --help exit 1 else # Not a recognized option. Assume it's the first argument break endif end # No arguments defined, so allow none if ($#argv > 0) then $0 --help exit 1 endif # Collect options from files set scalejpg_options_string = "-height 225" set scalejpg_options_file = ".thumbnail_options" if (-e $scalejpg_options_file:q) then set scalejpg_options_string = `cat $scalejpg_options_file:q` endif mkdir thumbs >& /dev/null foreach image_file (*.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG]) # Force file extension of any existing thumbnails to lowercase for # consistency across all slideshows, even those originally created # with uppercase or mixed case extensions # This also deletes extra thumbnail files when the same one exists # with multiple variations on case in the extension. Doesn't really # matter which one we keep since they SHOULD be identical, and if # the image file is newer, we're going to generate a new thumbnail # anyhow. # Note: Have to test for the existence of thumb files with various # cases with a foreach loop, not with "if (-e ...)" because # -e ignores case. set file_without_extension = $image_file:r:q set thumb_file = "thumbs/$file_without_extension.jpg" # Note: Have to check whether foreach will find any files. # Otherwise it reports: "foreach: No match." and aborts this # entire script. ls thumbs/$file_without_extension.[jJ][pP][gG] >& /dev/null if (! $status) then foreach existing_thumb_file (thumbs/$file_without_extension.[jJ][pP][gG]) if ("$existing_thumb_file" != "$thumb_file") then echo -n "Lowercase: " mv -v "$existing_thumb_file" "$thumb_file" endif end endif unset file_without_extension # Note: Tried to do with one case instead of 2, as: # if (! -e "$thumb_file" || $image_file:q == "`find $image_file:q -newer $thumb_file:q`") then # but the || didn't seem to shortcut as expected. When the thumbs file # did not exist, the find command was still executed, so we saw an error # message like the following, even though it worked fine: # find: thumbs/2001-10-29_01_SunroomRight.jpg: No such file or directory # Since I had to split it into 2 cases anyhow, I can now echo "New" vs # "Updated". if (! -e "$thumb_file") then echo -n "New image: " (set echo; scalejpg $scalejpg_options_string "$image_file" "$thumb_file") else if ($image_file:q == "`find '$image_file:q' -newer '$thumb_file:q'`") then echo -n "Updated image: " (set echo; scalejpg $scalejpg_options_string "$image_file" "$thumb_file") endif # Overwrite the thumbnail with the original if the thumbnail contains # more bytes. Don't want thumbnails that are scaled bigger than the # original. Looks too pixelated. # -L = Use the size of the actual file, not the symlink @ image_size = `stat -L -f "%z" $image_file:q` @ thumb_size = `stat -L -f "%z" $thumb_file:q` if ($thumb_size > $image_size) then echo " Replacing too large thumb with original" cp -v $image_file:q $thumb_file:q endif end # Check for obsolete thumbs files that have no full-sized counterpart. cd thumbs foreach thumb_file (*.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG]) # Note: Put all the -o clauses at the end. Can't end with -print. # That causes it to find nothing. Why? # Also, can't put -print earlier. That causes it to find all. # Just leave -print out. It's implied anyhow. set image_file = `find .. -maxdepth 1 \ -name "${thumb_file:r}.[jJ][pP][gG]" \ -o -name "${thumb_file:r}.[gG][iI][fF]" \ -o -name "${thumb_file:r}.[pP][nN][gG]" \ ` if ("$image_file" == "") then echo "Deleting obsolete thumbnail $thumb_file..." rm -v $option_interactive_string "$thumb_file" endif end