Python 截图 2+监视器(窗口)

发布于 2024-11-28 01:08:59 字数 714 浏览 2 评论 0原文

如果连接到多个显示器,如何使用 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 中有获取完整屏幕截图的选项吗?

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

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆头 2024-12-05 01:08:59

我的 Desktopmagic 库为 Python 2.6、2.7 和 3.3+ 提供了此功能。它可以返回 PIL/Pillow 图像或写入 BMP。

My Desktopmagic library provides this functionality for Python 2.6, 2.7, and 3.3+. It can return a PIL/Pillow Image or write a BMP.

睫毛上残留的泪 2024-12-05 01:08:59

混合使用 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)

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)
極樂鬼 2024-12-05 01:08:59
  • 安装desktopmagic:
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]) 
  • 0 用于第一个显示器 1 用于第二个显示器,...
#saves screenshot
rect.save('leftscr.png',format='png')
  • install desktopmagic:
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())
  • take a screenshot of the second monitor
rect = getRectAsImage(screens[1]) 
  • 0 for the first display 1 for the second and ...
#saves screenshot
rect.save('leftscr.png',format='png')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文