5

I am working on a project to take screenshots of desktop and upload images to a server I want to develop a python app, when user starts that app it will start taking screenshots in random interval like between 2-5 min of interval I have the code that will take screenshot and I have tested it on ubuntu it is working properly. Code to capture screenshot

import gtk.gdk
import time

w = gtk.gdk.get_default_root_window()
sz = w.get_size()
print "The size of the window is %d x %d" % sz
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])

ts = time.time()
filename = "screenshot"
filename += str(ts)
filename += ".png"

if (pb != None):
    pb.save(filename,"png")
    print "Screenshot saved to "+filename
else:
    print "Unable to get the screenshot."

I want to run this code in random interval of 2-5 min how can I develop a script that will run this code in thread.

2

2 Answers 2

4
#!/usr/bin/env python

import gtk.gdk
import time
import random

while 1 :
    # generate a random time between 120 and 300 sec
    random_time = random.randrange(120,300)

    # wait between 120 and 300 seconds (or between 2 and 5 minutes)
    print "Next picture in: %.2f minutes" % (float(random_time) / 60)
    time.sleep(random_time)

    w = gtk.gdk.get_default_root_window()
    sz = w.get_size()
    print "The size of the window is %d x %d" % sz
    pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
    pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])

    ts = time.time()
    filename = "screenshot"
    filename += str(ts)
    filename += ".png"

    if (pb != None):
        pb.save(filename,"png")
        print "Screenshot saved to "+filename
    else:
        print "Unable to get the screenshot."
2
  • yes this is working.. Sep 13, 2013 at 11:40
  • this does not work in GUI based application, please check updated question. Sep 30, 2013 at 10:07
2

I would be tempted to farm this out to an external library like PyScreenshot.

Let it handle the heavy lifting (like working out which DM and screen API it needs), leaving you with a PIL image that you can either save to disk or stream out as an upload.

There are a bevy of answers doing this manually on Stack Overflow though, if you're determined to DIY.


As for the interval, does this have to be threaded? Can the script not just run on its own and then after it's taken a snapshot, sleep for a random period (between 120 and 300 seconds)?

import random
import time

time.sleep(random.randrange(120, 300))
3
  • Actually I want to keep this script running till the user logged in to this app like Odesk team tool or elance time tracker. Sep 13, 2013 at 11:05
  • Please suggest whether I should use threaded approach or just run it in a loop and sleep for some random interval Sep 13, 2013 at 11:06
  • If this is running on its own, sleep is fine. If it's part of a bigger script that's doing other stuff, stick all the screenshot code in its own thread and keep the time.sleep() in the thread code (so the thread keeps running as long as the main process).
    – Oli
    Sep 13, 2013 at 11:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .