python如何用ImageGrab 获取两个显示器的屏幕截图保存到本地,尤其是第二块屏幕的内容
https://github.com/ludios/Desktopmagic
Desktopmagic在Windows上拍摄屏幕截图。 它支持任何配置的多台显示器,并且在任何故障模式下(锁定工作站,未连接显示器等)都不会泄漏内存。 如果您愿意,可以连续使用它来拍摄数百万个屏幕截图。
您可能希望使用它而不是PIL的ImageGrab,因为:
Desktopmagic可以截取所有监视器的屏幕截图。 您可以:
拍摄整个虚拟屏幕的屏幕截图。
拍摄整个虚拟屏幕的屏幕快照,每个显示分为一个PIL图像。
截屏仅显示一个屏幕。
拍摄虚拟屏幕任意区域的屏幕截图。
pip install --user Desktopmagic
from __future__ import print_function
from desktopmagic.screengrab_win32 import (
getDisplayRects, saveScreenToBmp, saveRectToBmp, getScreenAsImage,
getRectAsImage, getDisplaysAsImages)
# Save the entire virtual screen as a BMP (no PIL required)
saveScreenToBmp('screencapture_entire.bmp')
# Save an arbitrary rectangle of the virtual screen as a BMP (no PIL required)
saveRectToBmp('screencapture_256_256.bmp', rect=(0, 0, 256, 256))
# Save the entire virtual screen as a PNG
entireScreen = getScreenAsImage()
entireScreen.save('screencapture_entire.png', format='png')
# Get bounding rectangles for all displays, in display order
print("Display rects are:", getDisplayRects())
# -> something like [(0, 0, 1280, 1024), (-1280, 0, 0, 1024), (1280, -176, 3200, 1024)]
# Capture an arbitrary rectangle of the virtual screen: (left, top, right, bottom)
rect256 = getRectAsImage((0, 0, 256, 256))
rect256.save('screencapture_256_256.png', format='png')
# Unsynchronized capture, one display at a time.
# If you need all displays, use getDisplaysAsImages() instead.
for displayNumber, rect in enumerate(getDisplayRects(), 1):
imDisplay = getRectAsImage(rect)
imDisplay.save('screencapture_unsync_display_%d.png' % (displayNumber,), format='png')
# Synchronized capture, entire virtual screen at once, cropped to one Image per display.
for displayNumber, im in enumerate(getDisplaysAsImages(), 1):
im.save('screencapture_sync_display_%d.png' % (displayNumber,), format='png')