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
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?



Worth noting that ImageGrab
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/ImageGrab.py", 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.
http://wiki.wxpython.org/WorkingWithImages#A_Flexible_Screen_Capture_App
Sure.. that's written in the
Sure.. that's written in the article.. thanks for suggesting an alternative.
wxPython Screen Capture module
I've written a cross-platform screen capture tool in wxPython.
See download "GeneralScreenShotWX.py" in subtopic "A Flexible Screen Capture App"
under topic "WorkingWithImages"
@ http://wiki.wxpython.org/WorkingWithImages#A_Flexible_Screen_Capture_App.
It's working on MSW, OS X and one Linux distro and requires no extra wxPython packages.
Ray Pasco
Thanks Ray for the pointer.
Thanks Ray for the pointer.
Awesome!
Awesome, thank you! Yet another reaso to love python
Error
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
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
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
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
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.
Thanks for your contribution.
Figo! Python lo sto
Figo! Python lo sto imparando in questo periodo!
Post new comment