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.

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."
| improve this answer | |
  • yes this is working.. – Subodh Ghulaxe Sep 13 '13 at 11:40
  • this does not work in GUI based application, please check updated question. – Subodh Ghulaxe Sep 30 '13 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))
| improve this answer | |
  • Actually I want to keep this script running till the user logged in to this app like Odesk team tool or elance time tracker. – Subodh Ghulaxe Sep 13 '13 at 11:05
  • Please suggest whether I should use threaded approach or just run it in a loop and sleep for some random interval – Subodh Ghulaxe Sep 13 '13 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 '13 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.