如果连接到多个显示器,如何使用python制作屏幕截图?
我试过了:
import sys
from PyQt4.QtGui import QPixmap, QApplication
app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save('test.png', 'png')
import ImageGrab
im = ImageGrab.grab()
im.save('test.png', 'PNG')
这两个选项都提供了屏幕截图,只提供了主显示器
如果我使用winapi:
hWnd = win32gui.FindWindow(None, win_name)
dc = win32gui.GetWindowDC(hWnd)
i_colour = int(win32gui.GetPixel(dc,int(x),int(y)))
rgb = ((i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff))
我从第二台显示器的窗口得到一张照片。但它会很慢。
如果我按下剪贴板中的'printscreen'键将是一个正常的屏幕截图,包括所有显示器。有没有选择在Python中获得完整的屏幕截图?
答案 0 :(得分:7)
我的Desktopmagic库为Python 2.6,2.7和3.3+提供了此功能。它可以返回PIL /枕头图像或写入BMP。
答案 1 :(得分:5)
混合使用wxPython,win32api和ctypes:
import wx, win32api, win32gui, win32con, ctypes
class App(wx.App):
def OnInit(self):
dll = ctypes.WinDLL('gdi32.dll')
for idx, (hMon, hDC, (left, top, right, bottom)) in enumerate(win32api.EnumDisplayMonitors(None, None)):
hDeskDC = win32gui.CreateDC(win32api.GetMonitorInfo(hMon)['Device'], None, None)
bitmap = wx.EmptyBitmap(right - left, bottom - top)
hMemDC = wx.MemoryDC()
hMemDC.SelectObject(bitmap)
try:
dll.BitBlt(hMemDC.GetHDC(), 0, 0, right - left, bottom - top, int(hDeskDC), 0, 0, win32con.SRCCOPY)
finally:
hMemDC.SelectObject(wx.NullBitmap)
bitmap.SaveFile('screenshot_%02d.bmp' % idx, wx.BITMAP_TYPE_BMP)
win32gui.ReleaseDC(win32gui.GetDesktopWindow(), hDeskDC)
return False
App(0)
答案 2 :(得分:1)
pip install Desktopmagic)
from __future__ import print_function
import desktopmagic
from desktopmagic.screengrab_win32 \
import(getDisplayRects,saveScreenToBmp,getScreenAsImage,getRectAsImage,getDisplaysAsImages)
""" getDisplayRects functions returns a list with all displays, in display order, like [(0, 0, 1280, 1024), (-1280, 0, 0, 1024), (1280, -176, 3200, 1024)] : (left, top, right, bottom)"""
screens=(getDisplayRects())
rect = getRectAsImage(screens[1])
#saves screenshot
rect.save('leftscr.png',format='png')