1

I'm currently able to take a screenshot of my desktop using this code

from PIL import Image, ImageGrab
x =ImageGrab.grab()
x.show()

but the problem is, this captures the python script/idle dialog(box) which is running the script as well. I want to take a clean screenshot of my desktop screen, caputuring all the desktop icons,background screen and taskbar etc via a script.

Is there a way to automate it so the script can automatically do this?

Thanks.

Moondra
  • 4,399
  • 9
  • 46
  • 104
  • Did you manage to get it working? – Eddie Dec 29 '16 at 07:45
  • Hi Eddie, I haven't a chance to test it out yet. I'm finishing up a couple of other scripts, first. I don' t have much experience using web servers, so I want to do a some reading on the subject and go through a few tutorials, just to get a basic understanding. I will let you know how it goes once I play around with it. Really appreciate the tips and insights. – Moondra Dec 29 '16 at 11:24

2 Answers2

1

One of the solutions would be to use a Python web server (tornado or whatever).

Take a look at the following code (Python 3 version):

pip install tornado

from PIL import ImageGrab
import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        x = ImageGrab.grab()
        x.show()
        self.write("Hello, world")


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Minimise the IDLE window when the script is running.

Open a browser from your phone or another computer and navigate to:

http://[your IP address]:8888

I cannot test this script because ImageGrab.grab() only works on Mac and Windows but unfortunately not on Linux.

Eddie
  • 1,043
  • 8
  • 14
  • Thanks. I will read a bit more on python servers as I'm not too familiar with them. – Moondra Dec 25 '16 at 04:32
  • It should be pretty easy. Once you `pip install tornado` from the terminal window, you can literally copy, paste and run the code above. When you navigate to the link above, the code inside the get function executes. If you want to learn more about Tornado here is the docs (http://www.tornadoweb.org/en/stable/). It is relatively straight forward. I strongly recommend you to use a proper IDE like PyCharm (if you haven't already) and run your app in a debug mode with a bunch of break points. It will make your life easier. – Eddie Dec 25 '16 at 07:25
1

I tried out this code:

from PIL import ImageGrab
import time
import cv2

time.sleep(5)
x = ImageGrab.grab() 
x.save('Screenshot.jpg')
x.show()

I delayed the execution of ImageGrab.grab() by 5 seconds, in the meantime I minimized my workspace and switched to my Desktop. After 5 seconds it captured a screenshot of my desktop and saved it.

I am still figuring out a way to automatically capture the Desktop.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • You can check [THIS POST](http://stackoverflow.com/questions/24129253/screen-capture-with-opencv-and-python-2-7/24213099#24213099) as well. – Jeru Luke Jan 03 '17 at 12:13