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

ImageGrab.grab().save("screen_capture.jpg", "JPEG")

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

Easy, isn't it?

Posted in:

Worth noting that ImageGrab

Submitted by Sepero (not verified) on Tue, 2012-04-10 14:21.

Worth noting that ImageGrab only works on MSWindows.

On Ubuntu Linux I get this:
>>> import ImageGrab
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.7/dist-packages/PIL/", line 34, in
import _grabscreen
ImportError: No module named _grabscreen

I love Python also, so for cross platform compatibility, a person would probably be best off with the wxPython library.

Sure.. that's written in the

Submitted by fabio on Tue, 2012-04-10 14:50.

Sure.. that's written in the article.. thanks for suggesting an alternative.

wxPython Screen Capture module

Submitted by Ray Pasco (not verified) on Mon, 2011-06-06 04:26.

I've written a cross-platform screen capture tool in wxPython.
See download "" in subtopic "A Flexible Screen Capture App"
under topic "WorkingWithImages"

It's working on MSW, OS X and one Linux distro and requires no extra wxPython packages.

Ray Pasco

Thanks Ray for the pointer.

Submitted by fabio on Mon, 2011-06-06 08:07.

Thanks Ray for the pointer.


Submitted by Niels Heulin (not verified) on Wed, 2011-01-05 16:48.

Awesome, thank you! Yet another reaso to love python


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

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:


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!