Capturing a screen image with python and PIL on Windows

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?

Scroll to Top