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.
No comments:
Post a Comment