PIL和Python27 - 保存一个screengrab?

时间:2013-05-22 06:45:59

标签: python python-2.7 screenshot python-imaging-library clipboard

这是代码

from PIL import ImageGrab
scrgrb = ImageGrab.grabclipboard()
scrgrbnum = 0

def saveimg():
    scrgrb.save("screengrab.jpeg + str(scrgrbnum + 1)", "JPEG")
    scrgrbnum = scrgrbnum + 1

saveimg()

我收到此错误

Traceback (most recent call last):   File
 "C:/Python27/Programs/screengrab", line 10, in <module>
     saveimg()   File "C:/Python27/Programs/screengrab", line 7, in saveimg
     scrgrb.save("screengrab.jpeg + str(scrgrbnum + 1)", "JPEG") AttributeError: 'NoneType' object has no attribute 'save'

为什么我不能保存screengrab对象?

2 个答案:

答案 0 :(得分:1)

尝试

 import win32api, win32con, ImageGrab
 win32api.keybd_event(win32con.VK_SNAPSHOT, 1)
 im = ImageGrab.grabclipboard()
 im.save("screenshot.jpg", "JPEG")

我认为这里的关键是导入winapi。

Source

答案 1 :(得分:1)

好的,我找到了答案,显然是

scrgrb = ImageGrab.grabclipboard()

实际应该是

scrgrb = ImageGrab.grab()

我在这里找到了最常见的答案Python windows 7 screenshot without PIL

PIL网站上的文档未显示此更改-.-