Quick Screenshots Script (Python + PIL)
I know there are programs everywhere that take screenshots for you, but I figured I might as well write one myself too. It’s pretty neat that it’s only a couple lines of code in Python. I also like my version because I can tell it to automatically open up the image in whatever editor I desire and mark up by image usually to circle problems on websites…
To use, just copy this code below in a file on your computer, and ideally give it a .pyw extension so you don’t have a console open up. (I saved mine as screenshooter.pyw on my desktop)
Then put a shortcut in your quick launch toolbar, or anywhere else so long as you don’t have to minimize or cover up anything on your screen to get to it.
Feedback and corrections are welcome.
"""
Simple script to capture your screen, save it named with today's date and then
open it to allow editing and markup (circle problems, etc)
Created by Greg Pinero (gregpinero@gmail.com) Spring 2005
To run this you'll need:
>Python Imaging Library (PIL) -http://www.pythonware.com/products/pil/
>Python 2.3 or later - http://www.python.org
>Windows?
"""
import os
import sys
import time
import Image
import ImageGrab
#---------------------------------------------------------
#User Settings:
SaveDirectory=r'C:\Documents and Settings\Gregory\Desktop'
ImageEditorPath=r'C:\WINDOWS\system32\mspaint.exe'
#Here is another example:
#ImageEditorPath=r'C:\Program Files\IrfanView\i_view32.exe'
#---------------------------------------------------------
img=ImageGrab.grab()
saveas=os.path.join(SaveDirectory,'ScreenShot_'+time.strftime('%Y_%m_%d%_%H_%M_%S')+'.jpg')
img.save(saveas)
editorstring='""%s" "%s"'% (ImageEditorPath,saveas) #Just for Windows right now?
#Notice the first leading " above? This is the bug in python that no one will admit...
os.system(editorstring)






October 10th, 2005 at 2:10 pm
I suggest to use png instead of jpg.
It’s nearly half in size, and much better quality.
October 13th, 2005 at 4:22 am
~ >> python
Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import os
>>> import sys
>>> import time
>>> import Image
>>> import ImageGrab
Traceback (most recent call last):
File “”, line 1, in ?
File “/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/PIL/ImageGrab.py”, line 34, in ?
import _grabscreen
ImportError: No module named _grabscreen
>>>
October 13th, 2005 at 10:47 am
Mr. Required…
I haven’t tested this script on Macs. It could be that you just need to install PIL or research why your installation of PIL doesn’t have ImageGrab.py. Otherwise maybe there is some fundamental reason that this script won’t run on a Mac, in which case I emplore all Mac users everywhere to help me improve this script to make it more cross-platform!
-Greg
October 15th, 2005 at 5:51 pm
for windows I changed the launch call to allow python to finish and close using “start”
editorstring=’”start “%s” “%s”‘% (ImageEditorPath,saveas)
October 15th, 2005 at 6:06 pm
Good idea, Martin. Thanks.
October 28th, 2005 at 12:28 pm
Had the same problem as Mr Required. Reading the ImageGrab.py file:
##
# (New in 1.1.3) The ImageGrab module can be used to copy
# the contents of the screen to a PIL image memory.
#
# The current version works on Windows only.
#
And I’m using Linux… bugger.
September 24th, 2007 at 12:58 pm
The screenshot script works great for my purpose. Is it possible to include in this script emailing the screenshot file it creates?
September 24th, 2007 at 1:34 pm
Mark, glad you like it. Email would be a useful feature. I don’t have time to add it right away but I bet you could put a bid on MicroPlege to get it done. Or just ask on the Python newsgroup how to do it. Otherwise I can help you in a few weeks.
October 4th, 2007 at 8:07 pm
Hi,
My requirement is to capture the image of a web page. This works but in the case of a scrollable page, it doesn’t capture the whole web page…only whatever is visible is captured. Can this script be modified for my requirement ?
October 4th, 2007 at 10:32 pm
Krishna,
Here are some Firefox extensions you can try:
https://addons.mozilla.org/en-US/firefox/addon/1146
http://www.snissa.com/
https://addons.mozilla.org/en-US/firefox/addon/3408
December 11th, 2007 at 2:33 pm
Thanks a lot,
I just wrote a well-working program!
Greetings Markus
January 12th, 2008 at 7:34 pm
great script,
I’m pretty new to python, so this might be a really easy question
how come I always get
Traceback (most recent call last):
File “C:\Python25\screen.py”, line 4, in ?
import Image
ImportError: No module named Image
I tried it with
from PIL import Image
as well, but then it just siad No module named PIL.
how do I find out where I should put the I PIL folder so python calls the module correctly?
January 12th, 2008 at 7:50 pm
Anonymous, you should install PIL seperately: http://www.pythonware.com/products/pil/
They should have an installation program for windows that you can run.
January 13th, 2008 at 12:15 pm
I had run the installer, and PIL is installed in the “Python25\Lib\site-packages” folder.
I still get the ImportError.
I can try reinstalling it helps.
January 13th, 2008 at 11:30 pm
ah, found the problem
I was executing the screenshot script in python version 2.4 while PIL was installed for v. 2.5. sorry to bother