Friday, June 22, 2007

auto-convert images for blog

Sol and I take a lot of pictures. The camera automatically names them (with zero prefixed integer sequential filenames) but it's very easy for filenames to collide (two different days can have 000001.jpg files). I also take pictures at a rather high resolution. The resulting images take up a LOT of space.

I wrote a shell script that, with the help of GraphicsMagick (or ImageMagick) takes all those jpgs and creates two files, one a thumbnail (ending in -th.jpg) and one a large images (ending in -1024.jpg). The date of file transfer (from camera to laptop) is prepended to the image name so that filename collisions become impossible.
irec
I originally put the thumbnails and -1024 files in separate directories. Now I put them in the same directory so that the files are easy to upload to photobucket (they're in the same directory, so clicking on the Browse to select an image button brings me to the same directory. It's very easy this way to upload the thumbnail and the -1024 together).

I still keep the thumb directory around but I populate it with links from the "smaller" directory. Keeping thumb around doesn't really make sense anymore. It's not like I have programs or scripts that depend on existence or contents of that directory. Just laziness, I guess.

If the script were named "picstoblog" (see below) I would do something like the ff to process all the directories in my Pics directory.

for fn in *
do
if [ -d $fn ]
then
pushd .
cd $fn
picstoblog
popd
fi
done


----
picstoblog script below, some things could be improved (mainly be simplifying/eliding, e.g., thumb), but i don't care enough to actually do the fixes)
-----

#!/bin/bash

if [ ! -d thumb ]
then
mkdir thumb
else
rm -f thumb/*
fi

if [ ! -d smaller ]
then
mkdir smaller
fi

for fn in *.jpg
do
base=`basename $fn | cut -f 1 -d '.'`
d=`pwd | cut -f 1-3 -d "-" | sed "s/-//g"`
d=`echo $d | basename $d`

sm=$d-$base-1024.jpg
th=$d-$base-th.jpg

if [ ! -f smaller/$sm ]
then
gm convert -scale 1024x768 -quality "70%" $fn smaller/$sm
fi

if [ ! -f smaller/$th ]
then
gm convert -scale 200x150 -quality "50%" $fn smaller/$th
fi

# for older files, remove if already there so can link
rm -f thumb/$sm
ln -s smaller/$sm thumb/$sm
ln -s smaller/$th thumb/$th
done

No comments: