Deprecated: Assigning the return value of new by reference is deprecated in /f2/blendedtechnologies/public/wp-content/plugins/pmetrics.php on line 1192
Blended Technologies » Blog Archive » Quick Screenshots Script (Python + PIL)

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)

del.icio.us |  Digg |  FURL |  Yahoo! My Web 2.0 |  Reddit

15 Responses to “Quick Screenshots Script (Python + PIL)”

  1. Norbert Sebok Says:

    I suggest to use png instead of jpg.
    It’s nearly half in size, and much better quality.

  2. required Says:

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

  3. Administrator Says:

    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

  4. martin Says:

    for windows I changed the launch call to allow python to finish and close using “start”

    editorstring=’”start “%s” “%s”‘% (ImageEditorPath,saveas)

  5. Administrator Says:

    Good idea, Martin. Thanks.

  6. Carl Says:

    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.

  7. Mark Bratcher Says:

    The screenshot script works great for my purpose. Is it possible to include in this script emailing the screenshot file it creates?

  8. Greg - CEO/Founder Says:

    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.

  9. Krishna Says:

    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 ?

  10. Greg - CEO/Founder Says:

    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

  11. Markus Says:

    Thanks a lot,
    I just wrote a well-working program! :D

    Greetings Markus

  12. Anonymous Says:

    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?

  13. Greg - CEO/Founder Says:

    Anonymous, you should install PIL seperately: http://www.pythonware.com/products/pil/

    They should have an installation program for windows that you can run.

  14. Anonymous Says:

    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.

  15. Anonymous Says:

    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