Python 截图 2+ 显示器(windows) [英] Python screenshot 2+ monitors (windows)

查看:70
本文介绍了Python 截图 2+ 显示器(windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果连接到多个显示器,如何使用python制作屏幕截图?

How to make a screenshot with python, if connected to multiple monitors?

我试过了:

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

如果我使用 winapi:

If I use 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.

如果我按下剪贴板中的printscreen"键,将是一个包含所有显示器的普通屏幕截图.是否可以选择在 Python 中获取完整屏幕截图?

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?

推荐答案

混合使用 wxPython、win32api 和 ctypes:

Using a mix of wxPython, win32api and 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)

这篇关于Python 截图 2+ 显示器(windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆