Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
1  
Please ask one question per time. If you have another question, please use Ask Question button. It is not ok to edit your question to add more questions. –  Radu Rădeanu Sep 30 '13 at 10:19
    
please check here askubuntu.com/q/351830/192580 –  Subodh Ghulaxe Sep 30 '13 at 11:39
add comment

2 Answers 2

up vote 4 down vote accepted
#!/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."
share|improve this answer
    
yes this is working.. –  Subodh Ghulaxe Sep 13 '13 at 11:40
    
@SubodhGhulaxe If this answered your question, you may mark this answer as accepted, by clicking the green check mark next to it. That will indicate that your problem is solved. –  Radu Rădeanu Sep 16 '13 at 14:52
    
this does not work in GUI based application, please check updated question. –  Subodh Ghulaxe Sep 30 '13 at 10:07
add comment

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))
share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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