[size=medium][b]1.python截屏的方法:方法一使用PIL库,方法二使用win32[/b][/size]
PIL库截屏简单
[size=medium][b]2.PIL库截屏[/b][/size]
coordinate = (0, 0, 60,20)
pic = ImageGrab.grab(coordinate)
[size=medium][b]3.win32库截屏[/b][/size]
def grab(dpath,args):
'''''
参数为指定保存的目录
'''
hwnd = 0
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC=win32ui.CreateDCFromHandle(hwndDC)
saveDC=mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
MoniterDev=win32api.EnumDisplayMonitors(None,None)
#w = MoniterDev[0][2][2]
#h = MoniterDev[0][2][3]
w = args[2]
h = args[3]
#print w,h #图片大小
saveBitMap.CreateCompatibleBitmap(mfcDC, w,h)
saveDC.SelectObject(saveBitMap)
saveDC.BitBlt((0,0),(w, h) , mfcDC, (args[0],args[1]), win32con.SRCCOPY)
cc=time.gmtime()
bmpname=str(cc[0])+str(cc[1])+str(cc[2])+str(cc[3]+8)+str(cc[4])+str(cc[5])+'.bmp'
saveBitMap.SaveBitmapFile(saveDC, bmpname)
pic = Image.open(bmpname)
pic.save(os.path.join(dpath, 'xxxxx.jpeg'), 'jpeg')
os.remove(bmpname)
return pic
[size=medium][b]4.截屏的异常[/b][/size]
无论是使用win32还是PIL截屏,如果是远程登录windows服务器,然后退出用户的情况下都会报错,
[size=small][b]PIL[/b][/size]
File "E:\python-workspace\crawler-20150529\spiders\gz_spider.py", line 141, in _do_crawl_img_by_screenshot
pic = ImageGrab.grab(coordinate) # 截图
File "D:\Python27\lib\site-packages\PIL\ImageGrab.py", line 34, in grab
size, data = grabber()
IOError: screen grab failed
[size=small][b]win32[/b][/size]
File "E:\python-workspace\crawler-20150529\spiders\sele.py", line 162, in _do_crawl_img
img = self._do_crawl_img_by_screenshot()
File "E:\python-workspace\crawler-20150529\spiders\sz_spider.py", line 149, in _do_crawl_img_by_screenshot
pic = grab(self.storage_path,coordinate)
File "E:\python-workspace\crawler-20150529\GrabUtil.py", line 24, in grab
saveDC.BitBlt((0,0),(w, h) , mfcDC, (args[0],args[1]), win32con.SRCCOPY)
error: BitBlt failed
[size=small][b]错误原因:[/b][/size]mstsc远程登录服务器的时候用户处于登录状态,断开远程界面用户处于登出状态,计算机无用户登录状态时整个屏幕是黑的,不能正常截屏
[size=small][b]解决方法:[/b][/size]1.使用svc登录部署,这样用户一直处于登录状态。2.多用户登录,其中一个用户一直处于登录,部署时使用另一个用户部署