Capturing a screen image with python and PIL on Windows

Submitted by fabio on Tue, 2009-03-17 12:11.

The more I use Python, the more I love it.

This programming language is simply awesome: with it's simple api and the huge amount of freely available additional modules creating complex applications is really easy and fast.

For example less than 15 minutes ago I wondered: how can I capture an image of the computer screen using Python?

If you don't know Python you might think that this could be pretty a complex task.

Well.. given that you have the Python Imaging Library (PIL) installed and you are runnning Windows (I will look into a way of doing that on Linux) it turns out that it's simply a 4 lines of code matter:

"""
A simple screen grabbing utility

@author Fabio Varesano - 
@date 2009-03-17
"""


from PIL import ImageGrab
import time

time.sleep(5)
ImageGrab.grab().save("screen_capture.jpg", "JPEG")

Once you save the above code into a file named screen_grab.py (or anything else you want) and then run from the command line, being in the directory where you placed that file, the command python screen_grab.py an image named screen_capture.jpg with your screen capture will be created.

Easy, isn't it?

Error

Submitted by Vitali (not verified) on Fri, 2010-03-12 23:24.

I was wondering how to capture screenshot with python as well, however I get error when using PIL:

IOError: encoder jpeg not available

This means that somehow the

Submitted by fabio on Sat, 2010-03-13 08:28.

This means that somehow the jpeg encoder in your Python installation isn't available.

You should ask for help to the developers who compiled your Python installation. You can ask for help to the Python Image-SIG mailing list.

Also, you might want to try using another image format: you could try with png for example.

You could use something like the following as last line:

ImageGrab.grab().save("screen_capture.png", "PNG")

ok nice, is there anyway to

Submitted by Anonymous (not verified) on Fri, 2010-01-15 18:19.

ok nice, is there anyway to focus on a particular area like a box like point(x1,y1) to poing(x2,y2) and a box would be formed and that would be captured?

Note that the call to

Submitted by fabio on Fri, 2010-01-15 19:32.

Note that the call to ImageGrab.grab() will return a python Image object which implements the crop() method.

So, you should be able to use something like the following:

from PIL import ImageGrab
import time

time.sleep(5)
box = (100,100,400,400)
ImageGrab.grab().crop(box).save("screen_capture.jpg", "JPEG")

I didn't test this. It should work, let me know if it won't work.
Hope this helps.

No need to crop (or to

Submitted by William (not verified) on Thu, 2010-02-25 12:39.

No need to crop (or to specify the save format if you pass a filename with a meaningful extension): all you need is something like:

ImageGrab.grab(box).save('box.png')

Thanks for your contribution.

Submitted by fabio on Thu, 2010-02-25 13:48.

Thanks for your contribution.

Figo! Python lo sto

Submitted by Matteo bovetti (not verified) on Sat, 2009-04-04 00:03.

Figo! Python lo sto imparando in questo periodo!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <img> <h2> <h3> <h4> <b>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may insert videos with [video:URL]

More information about formatting options