Saturday, February 4, 2012

Automate Desktop screenshoting in Ubuntu

Today I'll present (and explain) a tiny script used to create a screenshot of the active window in your Ubuntu installation.
Naturally, you can easily press Print Screen key and take the screenshot with the native Gnome-screenshot, but automating screen capture can be good in case you wish to stream your desktop or active program to some other computer - but still wish to avoid CPU intensive programs that stream it live.

I will be using ImageMagick (import), which you can easily get via apt, if it is not already installed on your Ubuntu.

Here is the script:
#!/bin/bash
echo "taking screenshots"
while :
do
        import -window root -resize 1920 Screenshot.png
        sleep 10
done
Save it and execute in Terminal with "bash scriptname" (eg.: bash /home/user/scriptname). You could also make it executable either via chmod +x in Terminal or by ticking it in the file options accessible via right click.

As you can see, it is a bash script. Echo will output the text between quotes, while : do signals the beginning of the endless loop (which means this screen will be canceled upon kill signal, either a CTRL+C or kill pid in Terminal).
Import -window root -resize 1920 Screenshot.png will screenshot your active window (your browser, a program, etc), resize it to 1920x1080 pixels and save it as Screenshot.png in your home (~/) folder.
Sleep 10 is the delay (in seconds), and since it is the last line in the loop, it is the delay until the loop repeats itself. You may switch that as you see fit.

Enjoy!

9 comments: