Bash Scripts
Image Processing Scripts
I have created some image processing bash scripts which rely on the ImageMagick package. The allow me to simply run the scripts and create the images ready for my website.
Name Processing
A big problem is that the filenames on my USB memory always has upper case filenames, which I hate. This script converts the filenames to lower case.
#!/bin/bash for name in "$@"; do newname=`echo $name | tr 'A-Z' 'a-z'` mv -v "$name" "$newname" done
Converting sources to website size
I use the following script to auto rotate the jpegs based on the exif meta data.
#!/bin/bash
# see http://www.impulseadventure.com/photo/exif-orientation.html
for img in "$@";
do
export orientation=`identify -format "%[EXIF:Orientation]" "$img"`
echo Processing $img $orientation
if [ $orientation == 1 ]
then
echo $img is already correct
else
if [ $orientation == 6 ]
then
echo $img is 6 rotating
convert -rotate 90 -quality 100 "$img" "$img"
fi
if [ $orientation == 8 ]
then
echo $img is 8 rotating
convert -rotate -90 -quality 100 "$img" "$img"
fi
fi
#convert -rotate 90 -quality 100 "$img" "$img"
done
All my cameras are over 5 megapixel and the jpgs are very large. I always scale the image to be 800x600 or 600x800. The following script converts images which are Landscape to 800x600 and Portrait to 600x800.
#!/bin/bash
for img in "$@";
do
export height=`identify -format %h "$img"`
export width=`identify -format %w "$img"`
let ratio=$width/$height
echo Image $img = [ $width x $height ] = $ratio
if [ $ratio == 0 ]
then
echo Portrait 0
convert "$img" -geometry 600x800 -format jpeg -quality 80 "$img"
else
echo Landscape 1
convert "$img" -geometry 800x600 -format jpeg -quality 80 "$img"
fi
done
Creating thumbnails
Once I have all the images I need to create the thumbnails, this creates the thumbnails. Any landscape images get thumbnails by simply scaling the image down. Portrait images have thumbnails created by taking the center of the image and creating a thumbnail of 175x109. This is my standard thumbnail size on all my pages.
#!/bin/bash
for img in "$@";
do
export name=`echo $img | awk 'BEGIN {FS="."} {
val = $1
x = 1
while ( x<(NF-1) ) {
x++
val = val"."$x
}
print val
}'`
export height=`identify -format %h "$img"`
export width=`identify -format %w "$img"`
let ratio=$width/$height
echo Image $name = [ $width x $height ] = $ratio
if [ $ratio == 0 ]
then
echo Portrait 0
convert "$img" -geometry x175 -format jpeg -quality 50 "$name"_s.jpg
else
if [ $ratio == 1 ]
then
echo Landscape 1
convert "$img" -geometry 175x -format jpeg -quality 50 "$name"_s.jpg
else
echo Landscape WIDE
convert "$img" -geometry 550 -format jpeg -quality 80 "$name"_s.jpg
fi
fi
done
Video Processing
#!/bin/bash
# Ideal mpeg4 options found somewhere
#opt="vbitrate=2160000:mbd=2:keyint=132:v4mv:vqmin=3:lumi_mask=0.07:\
#dark_mask=0.2:scplx_mask=0.1:tcplx_mask=0.1:naq"
opt=""
# allow 4 motion vectors per macroblock,
opt="${opt}:v4mv"
# very high quality
opt="${opt}:vhq"
# low quatlity
opt="${opt}:vbitrate=1000000"
# bit rate is ok if you are scaling down later
#opt="${opt}:vbitrate=2160000"
# very good quality
#opt="${opt}:vbitrate=5000000"
# orger of vf options are important
# Apply Donald Graft's adaptive kernel deinterlacer and
vf="-vf kerndeint"
# scale to qpal: 352x288 (PAL quarter screen)
#vf="${vf},scale=352:288"
# apply a high precision/quality denoise 3D filter
vf="${vf},hqdn3d"
function usage() {
echo "$0 <source.avi> <dest.avi>"
}
export source=$1
export destination=$2
if [ -z $source ]
then
usage
exit 0
fi
if [ -z $destination ]
then
usage
exit 0
fi
if [ -f $destination ]
then
echo "Error $destination already exists. WILL not overwrite"
exit 0
fi
trap "echo 'Control-C - exiting $0' && exit 0" INT
if [ -f $source ]
then
echo "Encoding $source to $destination"
mencoder $source $vf -ovc lavc -lavcopts vcodec=mpeg4:vpass=1$opt:acodec=mp3:abitrate=128 \
-oac lavc -o $destination
mencoder $source $vf -ovc lavc -lavcopts vcodec=mpeg4:vpass=2$opt:acodec=mp3:abitrate=128 \
-oac lavc -o $destination
else
usage
fi
exit 1
Burn a DVD from a directory
Script to check a directory will fit on a DVD
#!/bin/bash
function usage() {
echo "$0 <directory to burn>"
}
dir=$1
if [ -z $dir ]
then
usage
exit 0
fi
if [ -d $dir ]
then
du=`du -s "$dir"`
size=`echo $du | awk 'BEGIN {FS=" "} {print $1}'`
echo Directory is $size kb
# 4590208 DVD size
if [ $size -gt 4590208 ]
then
let over=4590208-$size
let over=`echo $over | sed 's/-//g'`
echo Directory $dir is $over kb oversize
else
let remain=$size-4590208
let remain=`echo $remain | sed 's/-//g'`
echo Directory $dir has $remain kb space
fi
else
echo Error $dir is not a directory or does not exist
usage
exit 0
fi
exit 1
Script to burn a directory to a DVD
#!/bin/bash
function usage() {
echo "$0 <directory to burn>"
}
dir=$1
if [ -z $dir ]
then
usage
exit 0
fi
if [ -d $dir ]
then
echo Burning $dir
growisofs -dvd-compat -Z /dev/dvdrw1 -J -r $dir
echo Waiting for burn to complete
sleep 5
mount /mnt/cdrom2
sleep 1
ls /mnt/cdrom2
umount /mnt/cdrom2
fi