Sunday, February 14, 2010

Feeding images to a digital picture frame

My digital picture frame has a limited amount of build in memory. In order to put more pictures on the frame, I choose to resize them to the frame's native resolution of 800x480.

I' m pretty sure that there are applications that manage image collections on Linux and also do image resizing. However, I prefered to use ImageMagic and some scripting to do the job.

My script,, takes files from a source directory. These may be images and videos mixed together. Then it resizes JPEG files and copies all others (possibly videos) to the current directory. The target resolution for the images is hard coded to 800x480 in the script, but changing this is trivial.

Here is a usage example. We have the original folder as taken from the digital camera in 'camera' and a destination folder 'picture_frame' that will consist of the contents to place on the frame.

$ ls
camera/ picture_frame/
$ cd picture_frame
$ ../camera/*

... when done, you can browse through picture_frame to see the result.

# Written by Michalis Giannakidis - 2009 -
# Released in the public domain as is, without any warranties. Feb 2010

set resolution="800x400"

if ( $# == 0 ) then
echo "Convert images to $resolution and copy to current directory."
echo "usage: `basename $0` source_files"
echo "example: `basename $0` ../original/*"
echo "Current directory needs to be different than the source."

set counter = 0

foreach i ( $* )
set counter = `expr $counter + 1`

set bfile = `basename $i`
set bdest = `python -c 'import sys; print sys.argv[1].decode("utf-8").lower().encode("utf-8"),' $bfile`

if ( -f s_$bdest || -f $bfile || -f s_$bfile ) then
echo $counter/$# s_$bdest exists. I refuse to copy.

file $i |grep -q "JPEG image data"
if ( $status == 0 ) then
echo $counter/$# s_$bdest
convert $i -resize $resolution^ -gravity center -extent $resolution s_$bdest
echo $counter/$# $bdest copied.
cp -f $i $bdest