January 21, 2009

'Scroll animating' an image





Needing to create an animated image of an ECG scrolling, I wrote a bash script to create sequential frames from the image and then combine them into an animated gif, all using imagemagick.


#!/usr/bin/env bash

# Convert a rectangular image into an animated gif with
# sections from the image scrolling through

# Raja S
# Jan 2009
# GPL

nargs=$#
if [ $nargs -eq 0 ]
then
echo "Error - No arguments provided !"
echo ""
echo "animate converts rectangular image to a scrolling animated gif"
echo "Usage : "
echo " animate filename [width] [overlap] [delay] [loop]"
echo " width is width of final gif (pixels), default same as image height"
echo " overlap is overlap between successive 'frames', default is tenth of width"
echo " delay in time in hundredths of a second between frames, default is 20"
echo " loop is number of times to loop, default is 0 (loop infinitely"
exit
fi

# command line arguments - filename, width of gif (opt) and overlap (opt)
imagefile=$1
basefilename=${imagefile%.*}

# find image size using identify
imagesize=$(identify $imagefile | awk '{print $3}')
imagewidth=$(echo $imagesize | awk -Fx '{print $1}')
imageheight=$(echo $imagesize | awk -Fx '{print $2}')

# width of gif - default is image height
if [ $nargs -gt 1 ]
then
width=$2
else
width=$imageheight
fi

if [ $width -gt $imagewidth ]
then
echo "Error - Width of gif cannot be larger than width of input image !"
exit
fi

# overlap - default is tenth of width
if [ $nargs -gt 2 ]
then
overlap=$3
else
overlap=$(($width/10))
fi

# delay
if [ $nargs -gt 3 ]
then
delay=$4
else
delay=20
fi

# loop
if [ $nargs -gt 4 ]
then
loop=$5
else
loop=0
fi


# make the image frames
filecount=0
for i in $(seq 0 $overlap $(($imagewidth-$width)))
do
printf -v suffix '%06d' $((filecount++))
convertstring="$width""x""$imageheight""+""$i""+0"
filename="$basefilename""part""$suffix"".jpg"
convert -crop "$convertstring" "$imagefile" "$filename"
done

# combine into gif
convert -delay "$delay" -loop "$loop" "$basefilename""part*.jpg" "$basefilename""animation.gif"



If you want to try it out, you can download it here.
Example usage:


raja: ls /data/tmp/sample/
train.jpg

raja: ./animate.sh /data/tmp/sample/train.jpg

raja: rm /data/tmp/sample/trainpart*

raja: ls /data/tmp/sample/
trainanimation.gif train.jpg



Disclaimer: This is provided with all good intentions, but I cannot guarantee that it will cause not harm. Note that I do not delete the files to avoid inadvertent deletion of any important files. However, it is best to place the target image in a separate folder and then run the script.

January 3, 2009

Emacs - a beginner's setup


It's almost 5 months since I started using emacs on a regular basis. After beginning to use it as a platform to learn lisp (with SLIME), now I use emacs for coding python and bash, writing manuscripts (with markdown), doing statistics with R (using ESS), file and directory management and sometimes even to play chess on FICS.

Emacs, in my opinion is something anyone should give a whirl. It is open source, free and easily installable on any operating system. It is an absolutely amazing piece of software - give it a shot and if after a couple of months you decide to give up, atleast you have got a taste of what it is about. Here I will attempt to list a few things that will ease the introduction to emacs.

Note: Many of these instructions may be specific for Gnu-emacs on Linux. They may vary if you are using another emacs variant or another platform.

1. Start off with a good looking emacs

An oft-repeated complaint is that emacs looks bad, bad enough to deter a newcomer sometimes. But then, it doesn't have to ...

Use the latest emacs snapshot that allows use of your preferred fonts with nice antialiasing. In Ubuntu this means

sudo aptitude install emacs-snapshot-gtk emacs-goodies-el

Now you can use any font you prefer by modifying the Xresources file as detailed here.

2. Disable the splash screen, scrollbar and toolbar. Add to your .emacs -

(setq inhibit-startup-message t)
(tool-bar-mode -1)
(scroll-bar-mode -1)


It may seem surprising to consider disabling the scrollbar and toolbar. Maybe a case can be made for having these on while you wean yourself from the mouse, but I believe that there are greater dividends to doing this early and getting used to mouseless editing. I have preferred leaving the menu-bar on although I dont use the menu items, just because it sometimes allows me to easily refer to the shortcuts available, but again, it is worth considering disabling this too.

3. Syntax highlighting

Install color themes. In ubuntu, this is done easily by installing emacs-goodies-el. Now, add this in your emacs to have 'font-lock' on for syntax highlighting.

(global-font-lock-mode t)

After M-x, typing color-theme- and hitting tab will shows the completions available. Try out the different color-themes to decide what you want to use. To enable it automatically, add a line like this -

(require 'color-theme)
(setq color-theme-is-global t)
(color-theme-scintilla)


4. Enable line numbers

If you feel lost without them,

(require 'linum)
(global-linum-mode)

5. If you like to 'see' a selected region, you have to enable transient-mark-mode -

(setq-default transient-mark-mode t)

6. And if you have trouble copy-pasting between other windows and emacs, this worked for me -

(setq x-select-enable-clipboard t)

With these basic things out of the way, you have a decent emacs set up to start with. There are multiple options for further customization, but I will leave them for you to experiment with. Packages to definitely try out are ido-mode, pabbrev, bookmarks and yasnippet.

Loading image

Click anywhere to cancel

Image unavailable