Python 拍摄特定尺寸的屏幕截图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19697210/
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
Taking Screen shots of specific size
提问by Belrouk
What imaging modules for python will allow you to take a specific size screenshot (not whole screen)? I have tried PIL, but can't seem to make ImageGrab.grab() select a small rectangle and i have tried PyGame but i can't make it take a screen shot outside of it's main display panel
哪些 python 成像模块可以让你截取特定大小的屏幕截图(不是整个屏幕)?我尝试过 PIL,但似乎无法让 ImageGrab.grab() 选择一个小矩形,我也尝试过 PyGame,但我无法让它在主显示面板外进行屏幕截图
采纳答案by Sagar Rakshe
You can use pyscreenshotmodule.
The pyscreenshot
module can be used to copy the contents of the screen to a PIL
image memory or file.
You can install it using pip
.
您可以使用pyscreenshot模块。
该pyscreenshot
模块可用于将屏幕内容复制到PIL
图像存储器或文件中。您可以使用pip
.
$ sudo pip install pyscreenshot
Usage:
用法:
import pyscreenshot as ImageGrab
# fullscreen
im=ImageGrab.grab()
im.show()
# part of the screen
im=ImageGrab.grab(bbox=(10,10,500,500))
im.show()
# to file
ImageGrab.grab_to_file('im.png')
回答by abarnert
I have tried PIL, but can't seem to make ImageGrab.grab() select a small rectangle
我试过 PIL,但似乎无法让 ImageGrab.grab() 选择一个小矩形
What did you try?
你尝试了什么?
As the documentation for ImageGrab
clearly states, the function has a bbox
parameter, and:
正如文档中ImageGrab
明确指出的那样,该函数有一个bbox
参数,并且:
The pixels inside the bounding box are returned as an “RGB” image. If the bounding box is omitted, the entire screen is copied.
边界框内的像素作为“RGB”图像返回。如果省略边界框,则复制整个屏幕。
So, you only get the whole screen if you don't pass a bbox
.
因此,如果您不通过bbox
.
Note that, although I linked to the Pillow docs (and you should be using Pillow), old-school PIL's docs say the same thing:
请注意,虽然我链接到 Pillow 文档(您应该使用 Pillow),但老式 PIL 的文档也说了同样的话:
The bounding box argument can be used to copy only a part of the screen.
边界框参数可用于仅复制屏幕的一部分。
So, unless you're using a really, really old version of PIL (before 1.1.3, which I believe is more than a decade out of date), it has this feature.
因此,除非您使用的是非常非常旧的 PIL 版本(在 1.1.3 之前,我认为它已经过时了十多年),否则它具有此功能。
回答by Luca Interwick
1) Use pyscreenshot, ImageGrab works but only on Windows
1) 使用 pyscreenshot,ImageGrab 有效但仅适用于 Windows
2) Grab the image and box it, then save that image
2)抓取图像并将其装箱,然后保存该图像
3) Don't use ImageGrab.grab_to_file, it saves the full size image
3) 不要使用 ImageGrab.grab_to_file,它会保存完整尺寸的图像
4) You don't need to show the image with im.show if you just want to save a screenshot
4) 如果您只想保存屏幕截图,则无需使用 im.show 显示图像
import pyscreenshot as ImageGrab
im=ImageGrab.grab(bbox=(10,10,500,500))
im.save('im.png')
回答by Dmitriy
You could use Python MSS.
您可以使用Python MSS。
From documentation to capture only a part of the screen:
从文档到仅捕获屏幕的一部分:
import mss
import mss.tools
with mss.mss() as sct:
# The screen part to capture
monitor = {"top": 160, "left": 160, "width": 160, "height": 135}
output = "sct-{top}x{left}_{width}x{height}.png".format(**monitor)
# Grab the data
sct_img = sct.grab(monitor)
# Save to the picture file
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
print(output)