Batch image conversion, resizing and uploading

Some time ago I added this article Very simple image gallery here. In it I describe, as the name suggests, a very simple image gallery for basic use, such as showing off game screenshots etc., which is precisely what I use it for.

Recently I made many screenshots of a game, and for that I looked for an easier way to do all the work.
Initially what I did is copy all the images I wanted to a separate directory from my windows gaming box over to a samba shared linux directory. There I renamed the images from its original name to $number.jpg for the image ordering. Afterwards I used a Windows program (faststone image viewer 4.0) to convert the images from TGA to JPEG (Star Trek Online gives the option to save screenshots in the TGA format).

Once done, I’d upload these images manually to my remote server, and let the script in above’s linked article do the thumbnail creation for the display. Since I also link a few screenshots to a forum sometimes, I manually create a separate thumbnail, 30% of its original size, and upload them afterwards as well.

All well and good, but yesterday I wanted to do it with 111 screenshots, and since I felt a bit lazy to do the repetitive task, I decided to instead get it automatized.

What you’ll need is ImageMagick, which you can easily install by doing

sudo aptitude install ImageMagick

or, presumably

sudo yum install ImageMagick

(check for correct package name, only have done it for my Debian system)

Create a directory at the location of your choice, where you’ll keep the original screenshots to be processed.
Create the sub directories ‘completed’, ‘full’, ‘tn’, ‘tn_small’ (or name them as you want, of course 😉 )

Then use this script:

#!/bin/bash
E_WRONG_ARGS=85;
script_params="-g  -x ";

if [ "$#" -eq 0 ] ; then
    echo
    echo "Usage: `basename $0` $script_params"
    echo
    exit $E_WRONG_ARGS
fi

while getopts "g:x:" o
do case "$o" in
    g) game="$OPTARG" ;;
    x) x="$OPTARG" ;;
    \?) echo "Usage: `basename $0` $script_params"
esac
done

if [ $game == "dao" ] ; then
    loc1="dao/img";
    loc2="dao";
    loc3="dao/tn";
elif [ $game == "sto" ] ; then
    loc1="sto/img";
    loc2="sto";
    loc3="sto/tn";
elif [ $game == "da2" ] ; then
    loc1="da2/img";
    loc2="da2";
    loc3="da2/tn";
fi

if [ -f bb.txt ] ; then
    rm bb.txt;
fi

if [ $game == "sto" ] ; then
    dir="dir -1ct *.tga";
    tga=true;
else
    dir="dir -1ct *.jpg";
    tga=false;
fi

for file in `$dir` ; do
    echo "$file";
    file2="$x.jpg";

    if [ $tga == "true" ] ; then
        convert $file full/$file2;
    else
        mv $file full/$file2;
    fi
    convert -resize 30% full/$file2 tn/$file2;
    convert -resize 200 full/$file2 tn_small/$file2;
   
    mv $file completed/;
    echo "[url=http://section-nine.org/$loc1/$file2]
    [img]http://section-nine.org/$loc2/$file2[/img][/url]" >> bb.txt;
    x=$((x+1));
done

sleep 10;

scp full/* $destination:$destdir/$loc1;
scp tn/* $destination:$destdir/$loc2;
scp tn_small/* $destination:$destdir/$loc3;

What the script does is following:

It accepts the parameters -g (game) and -x (count), in this case I have it set up for dao (Dragon Age Origins), sto (Star Trek Online) and da2 (Dragon Age 2 – soon to be released), and for the x I just pass a number over, e.g. if the current last screenshot online is 123.jpg, then I pass 124 to -x, and all processed images will have the correct number for its name.

The directory’s read through `dir -1ct`. I use ‘-1ct’ here to order them one file per line per time modified.
Initially I had `dir -d`, but since those are read in as columns then, my screenshot order has been changed, and I had images mixed up with wrong numbers.

I use 3 locations on my remote box in the website directory in “loc1=dao/img” the full sized screenshots will be saved. The 30% sized screenshots for the BB links are saved to “loc2=dao”, and the 200 pixels wide small screenshots are saved to “loc3=dao/tn”. Adjust as you see fit. “loc2” is a bit of a weird location, but I was to lazy to change it from when I did it manually (since I’d have to change all the already posted BB posts etc.).

Incase the passed game parameter is sto, it’ll convert the TGA images to a JPG image in the ‘full’ directory, otherwise it’ll just move the image to ‘full/$x.jpg’.

Then the processed file will be used to create thumbnails, again using ImageMagick’s `convert`. Once the images are processed, I move the original screenshot to ‘completed’ to keep the image incase any fault occurs and has to be rerun, instead of manually copying all files over again from the windows box. Afterwards I write an entry into the bb.txt file with the full BB code to link the image into a forum with BB support

Once all images have been processed, I added a sleep 10 to make sure all’s done. In the initial run a few images were “resource unavailable” (or similar), so added the sleep to give it a rest 😉

Then comes the `scp` to the remote box. This of course you should adjust to your own needs.