截取完整屏幕截图 python
take full screenshot python
我正在尝试为我的屏幕截图。
我知道这个功能
pyautogui.screenshot()
这个功能的问题是它只能截取一个屏幕的屏幕截图。我正在尝试为所有可用屏幕(通常是两个)截取完整屏幕截图。但是,它似乎在这方面不起作用。
鉴于您想要使用 Windows 系统,我建议您使用 Desktopmagic,一个 Python 库。
这是一个例子:
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')
如果您介意,我会推荐另一个模块:MSS(您不需要 PIL 或任何其他模块,只需要 Python;它是跨平台的):
from mss import mss
with mss() as sct:
sct.shot(mon=-1, output="fullscreen.png")
如果您有兴趣,documentation会尝试解释更多内容。
使用对我有用的 pyscreenshot 库,我截取了所有屏幕的屏幕截图。
来源:https://pypi.org/project/pyscreenshot/
#-- include('examples/showgrabfullscreen.py') --#
import pyscreenshot as ImageGrab
if __name__ == '__main__':
# grab fullscreen
im = ImageGrab.grab()
# save image file
im.save('screenshot.png')
# show image in a window
im.show()
#-#
如果您不想打开 GUI,只需注释 im.show()
行即可。
我正在尝试为我的屏幕截图。
我知道这个功能
pyautogui.screenshot()
这个功能的问题是它只能截取一个屏幕的屏幕截图。我正在尝试为所有可用屏幕(通常是两个)截取完整屏幕截图。但是,它似乎在这方面不起作用。
鉴于您想要使用 Windows 系统,我建议您使用 Desktopmagic,一个 Python 库。
这是一个例子:
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')
如果您介意,我会推荐另一个模块:MSS(您不需要 PIL 或任何其他模块,只需要 Python;它是跨平台的):
from mss import mss
with mss() as sct:
sct.shot(mon=-1, output="fullscreen.png")
如果您有兴趣,documentation会尝试解释更多内容。
使用对我有用的 pyscreenshot 库,我截取了所有屏幕的屏幕截图。
来源:https://pypi.org/project/pyscreenshot/
#-- include('examples/showgrabfullscreen.py') --#
import pyscreenshot as ImageGrab
if __name__ == '__main__':
# grab fullscreen
im = ImageGrab.grab()
# save image file
im.save('screenshot.png')
# show image in a window
im.show()
#-#
如果您不想打开 GUI,只需注释 im.show()
行即可。