Python 截图 2+ 显示器(windows)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6941089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python screenshot 2+ monitors (windows)
提问by Echeg
How to make a screenshot with python, if connected to multiple monitors?
如果连接到多个显示器,如何使用python制作屏幕截图?
I tried:
我试过:
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')
Both options provide a screenshot, only the primary monitor
两个选项都提供屏幕截图,只有主监视器
If I use winapi:
如果我使用 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))
I get a picture from a window in the second monitor. But it will be very slow.
我从第二台显示器的窗口中得到一张照片。但是会很慢。
If I press key 'printscreen' in the clipboard will be a normal screenshot, with all monitors. Is there a option to get a Full screenshot in Python?
如果我按下剪贴板中的“printscreen”键,将是一个包含所有显示器的普通屏幕截图。是否可以选择在 Python 中获取完整屏幕截图?
采纳答案by fraca7
Using a mix of wxPython, win32api and ctypes:
混合使用 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)
回答by Ivan Kozik
My Desktopmagiclibrary provides this functionality for Python 2.6, 2.7, and 3.3+. It can return a PIL/Pillow Image or write a BMP.
我的Desktopmagic库为 Python 2.6、2.7 和 3.3+ 提供了此功能。它可以返回 PIL/Pillow Image 或编写 BMP。