Tuesday, June 2, 2015

take screen shots with terminal in Linux (bash script)

I recently needed to take screen shots in Linux with a tool automatically. I needed to have a 15 seconds time intervals between these screen captures and save them in a directory to do some image processing on them.

Since most linux distributaions use X Window system as their graphical system they are shipped with X utilities which can come handy here. xwd is a tool to  dump an image of an X window. It saves them in a special file format. but we can use pipes to convert it to for example png file format:

$ xwd | xwdtopnm | pnmtopng > /tmp/screenshot.png

We can have all of it as an bash script as well. name it myScript.sh for instance.

#!/bin/bash
# Produce a screen dump periodically and save as a JPEG
 

DELAY=15                   # seconds between screen dumps
DIR=/tmp/sceenshots  # directory to hold screen dumps
I=0                              # current image number
 

mkdir -p ${DIR}
while sleep $DELAY
 

do
xwd -root | xwdtopnm | pnmtopng >${DIR}/screendump.${I}.png
((I++))

done

just do

$chmod +x myScript.sh

and you are fine!

No comments: