I spent the day writing this little script, thought I'd share it with fellow Magickers just in case it helps.
(I have not spent any time in bash for years, so that's why it took so long, it also means this may have weird bugs. So far I am happy with it.)
What it does:
It takes a source image, makes a 'thumbs' directory next to it and then makes a png thumbnail within it.
Note 1: It ignores any file that is not a valid image (uses identify command for this.)
Note 2: It only makes the thumbs dir if it's not there.
Note 3: It only makes an actual thumbnail if the thumbnail does not exist, or if it's older than the source. (This is nice for a fire-and-forget approach. You just worry about your source images, any changes to them and another run of the script will update only those thumbnails.)
Note 4: It makes 128x128 png-8 files, but you can hack the convert line in the script to anything you like.
How to use it:
You could run it on a single file, but that would not be much use. I would advice using gnu's find command. Examples are included in the remarks.
Code: Select all
#!/bin/bash
## Licence: DWTFYW
## This program is free software. It comes without any warranty, to
## the extent permitted by applicable law. You can redistribute it
## and/or modify it under the terms of the Do What The F**k You Want
## To Public License, Version 2, as published by Sam Hocevar. See
## http://sam.zoy.org/wtfpl/COPYING for more details. *
## Author: Donn Ingle, Oct 2010, South Africa
## Notes:
## This script runs once per image file passed to it: i.e. you loop it from somewhere else.
## A extra 'thumbs' directory is made in the directory it is run from, you may want to rm -fr it.
## Example using find:
## find . \( -name NOTINTHERE -prune \) -o \( -name thumbs -prune \) -o -iname "*.png" -exec makethumbs {} \;
## That will 1) Skip the NOTINTHERE directory, 2) Skip the thumbs directories (This is vital, else you get recursions)
## 3) Look for all png files and then run this script on each.
## Example 2:
## find . \( -name wiki -prune \) -o \( -name thumbs -prune \) -o \( -iname "p_*" -prune \) -o \( -iname "*dev*" -prune \) -o -iname "*.*" -type f -exec makethumbs {} \;
## 1) Skip 'wiki' directory, 2) skip 'thumbs', 3) skip anything starting with p_ (or whatever you like) 4) skip anything with 'dev' in it
## 5) Looks for any kinds of files 6) makes sure they are files not directories 7) runs this script on each.
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` some_image_to_process"
echo "Will make a thumb/ dir and a thumbnail of a given png in it."
echo "Run from a top of a tree of images to thumbnail all image files below it."
echo "Tip: use find's exec to run this script."
exit $E_BADARGS
fi
paf="$1"
## Get out if this is not an image file
if ! identify "$paf" &>/dev/null; then exit 1; fi
filename=`basename "$1"`
cwd=`dirname "$paf"`
## Make thumbs if it's not there. Also touch the file to make date same so that we trigger thumb creation later on.
if [ ! -e "$cwd/thumbs" ]; then
mkdir "$cwd/thumbs"
touch "$paf"
fi
## make a string of the new thumbnail path and name
new_thumb_file="$cwd/thumbs/$filename"
## Test new thumb file against current image. If it's older, then we remake it>
## (This also covers the case where the thumbnail does not exist at all.)
if [[ "$paf" -nt "$new_thumb_file" ]]; then
convert -format png -thumbnail 128x128 -strip -quality 95 PNG8:"$paf" "$new_thumb_file" &>/dev/null
if [ $? -ne 0 ]; then
echo ERROR making $new_thumb_file
else
echo Made new thumb: $paf TO $new_thumb_file
fi
fi
Save the file somewhere. Call it makethumbs
Make it executable: chmod u+x makethumbs
Use it: ./makethumbs some_image_file
Best if you move it to somewhere on your system path, so that other scripts can run it (from find, for example.)
Using find:
I better put a word or two down here. Use find to run the above script. This will make your life a lot easier.
Example:
You are in a directory where you have an entire tree of source images (in sub-sub-sub dirs) and you want to make thumbnails for the whole shebang. Let's say you want to skip some stuff too -- the 'wiki' directory, any files that are previews and don't need work, "p_*", and any files that are tagged 'dev' in their names. For all the rest (images only) you want to make thumbs. Sure thing:
Code: Select all
find . \( -name wiki -prune \) -o \( -name thumbs -prune \) -o \( -iname "p_*" -prune \) -o \( -iname "*dev*" -prune \) -o -iname "*.*" -type f -exec makethumbs {} \;
Well, HTH and all. Any bugs, just post, let's sort them out!
\d