A simple python screen grabber

Ene Uran 1 Tallied Votes 3K Views Share

This simple Python code uses the Tkinter GUI toolkit and the Python Image Library (PIL) to create a program that allows you to grab an image of the display screen (or portion of it) and save it to an image file. Unfortunately, the ImageGrab.grab() method only works on a Windows computer. Sorry about that to all you good folks using PC Linux or Apple Unix. I guess we have to ask the PIL folks to get on the ball!

# use a modified Tkinter keylogger and PIL ImageGrab to
# grab the screen image and save to an image file
# the ImageGrab.grab() function works with Windows only!
# download Python Image Library (PIL) free from:
# http://www.pythonware.com/products/pil/index.htm
# tested with Python25 on Vista    EU     05/15/2007

import Tkinter as tk
import ImageGrab  # PIL

def key(event):
    """modified Tkinter keylogger"""
    if event.keysym == 'F7':
        root.bell()
        # don't show window, move to taskbar as icon
        root.iconify()
        # small delay to allow tk window to fade on LCDs
        for x in range(1000000):
            x = x + 77
        # this will grab the whole screen display and save it to a file
        img = ImageGrab.grab()
        img.save("screenImage1.jpg")
        # this will grab a 400 pixel wide and 300 pixel heigh partion
        # upper left corner screen coordinates x=0, y=0
        img = ImageGrab.grab((0, 0, 400, 300))
        img.save("screenImage2.jpg")

root = tk.Tk()
root.title('Screen Image Grabber')

str1 = """\
Key F7 will make this program move to the taskbar as an icon
and then take a image grab of the screen and save to a file
"""

tk.Label(root, text=str1, bg='yellow').pack()

root.bind_all('<Key>', key)

root.mainloop()
AndyGman 0 Newbie Poster

Hey, i am having troubles with the ImageGrab.grab(bbox). I get the geometry of the window, and use that as the bbox (after some manipulations) . sometimes i can save the screen shot, sometimes it gives me the error: "SystemError: tile cannot extend outside image" Anyone know how to fix this? here is some of the code i'm using:

def getsize(self):
self.master.update()
self.geo= self.master.geometry()
self.width=""
self.posx=""
self.posy=""
self.height=""
print self.geo
for letter in self.geo:
if letter=="x":
self.geo=self.geo[1:len(self.geo)]
break
else:
self.width=self.width+letter
self.geo=self.geo[1:len(self.geo)]
for letter in self.geo:
if letter=="+":
self.geo=self.geo[1:len(self.geo)]
break
else:
self.height=self.height+letter
self.geo=self.geo[1:len(self.geo)]
for letter in self.geo:
if letter=="+":
self.geo=self.geo[1:len(self.geo)]
break
else:
self.posx=self.posx+letter
self.geo=self.geo[1:len(self.geo)]
self.posy=self.geo
width=int(self.width)
height=int(self.height)
posx=int(self.posx)
posy=int(self.posy)
print (posx,posy,width,height)

self.master.destroy()
image=ImageGrab.grab((posx,posy,width,height))


image.save("image.jpg")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.