Tools for Updating an Online Image Gallery

- - posted in Linux | Comments

Not long ago I shared my bash script for creating thumbnails and manageable sized “big” images for an image gallery, or other image store. Well, today I had to update an existing gallery with a pretty significant number of new images. It occurred to me that I’d like to only convert new images rather than re-converting the whole lot. Then, I’d like to simply upload the new files to my server in some automated way.

So here’s the tools to do it. It assumes you have the same layout for your pictures on your local drive as I do, which is as follows.

  • RootDirectoryForGallery – This contains the full resolution pictures directly from your camera, and the following directories.
    • web – This is a sub directory of RootDirectoryForGallery which contains the “big” web version of your image, and the thumbnails directory described below.
      • thumbnails – This is a sub directory of web, and contains the thumbnail sized versions of the images.

The first step, is to copy your new pictures from the digital camera to your RootDirectoryForGallery. Then, we want to get a list of the files that are new, so we run the following from the RootDirectoryForGallery.

diff . web/ | grep 'Only in \.: d' | awk '{print $4}' | grep jpg >> 3-6-08_update.diff

This creates a file in the RootDirectoryForGallery named “3-6-08_update.diff” which contains a list of only the new pictures, one per line. Now, we want to use our image thumbnail script to convert only the new images to the “big” and thumbnail sized images. I discovered that I needed to make a change to my script in order to do this. Namely in the for loop I needed to enclose the command used to list files in “$()”. The modified script is shown below. Notice the “$($SEARCH)” where there was previously just “$SEARCH”.



if [ $# -lt 3 ]
echo "You must pass three arguments 1) The search string (usually *.jpg) 2) The destination size I.E. 500x374 3) The destination directory"
exit 1

for i in $($SEARCH)
        echo "Converting $i"
        convert -resize $SIZE $i -resize $SIZE +profile '*' $DEST$i

So with our edited script we convert the files.

/opt/ "cat 3-6-08_update.diff" "800x600" "web/"
/opt/ "cat 3-6-08_update.diff" "150×112 "web/_thb_"

Lastly, we want to upload just the changed files to our server. We’ll use rsync for this. The paths, server name, and user name have been changed to protect the innocent. ;–)

rsync -e ssh -av /path/to/local/copy/

And there you have it, with just a few simple commands you’ve created thumbnails of all of your images, and uploaded just the new ones to your server. Easy!