Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 拍摄特定尺寸的屏幕截图_Python - Fatal编程技术网

Python 拍摄特定尺寸的屏幕截图

Python 拍摄特定尺寸的屏幕截图,python,Python,python的哪些成像模块允许您拍摄特定大小的屏幕截图(而不是整个屏幕)? 我尝试过PIL,但似乎无法使ImageGrab。grab()选择一个小矩形 我也尝试过PyGame,但我无法让它在主显示面板外进行屏幕截图 我尝试过PIL,但似乎无法使ImageGrab。grab()选择一个小矩形 你试了什么 正如的文档中明确指出的,该函数有一个bbox参数,并且: 边界框内的像素作为“RGB”图像返回。如果省略边界框,则复制整个屏幕 因此,如果您没有通过bbox,则只能看到整个屏幕 请注意,虽然我链接

python的哪些成像模块允许您拍摄特定大小的屏幕截图(而不是整个屏幕)? 我尝试过PIL,但似乎无法使ImageGrab。grab()选择一个小矩形 我也尝试过PyGame,但我无法让它在主显示面板外进行屏幕截图

我尝试过PIL,但似乎无法使ImageGrab。grab()选择一个小矩形

你试了什么

正如的文档中明确指出的,该函数有一个
bbox
参数,并且:

边界框内的像素作为“RGB”图像返回。如果省略边界框,则复制整个屏幕

因此,如果您没有通过
bbox
,则只能看到整个屏幕

请注意,虽然我链接到了枕头文档(您应该使用枕头),但老式的PIL文档:

“边界框”参数只能用于复制屏幕的一部分

所以,除非您使用的是非常非常旧的PIL版本(1.1.3之前,我认为它已经过时十多年了),否则它具有此功能。

您可以使用模块
pyscreenshot
模块可用于将屏幕内容复制到
PIL
图像存储器或文件中。 您可以使用
pip
安装它

$ sudo pip install pyscreenshot
用法:

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')
1) 使用pyscreenshot,ImageGrab可以工作,但只能在Windows上工作

2) 抓取图像并将其装箱,然后保存该图像

3) 不要使用ImageGrab.grab\u to\u文件,它会保存完整大小的图像

4) 如果只想保存屏幕截图,则不需要使用im.show显示图像

import pyscreenshot as ImageGrab

im=ImageGrab.grab(bbox=(10,10,500,500))

im.save('im.png')
你可以用

从文档到仅捕获屏幕的一部分:

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)

您可以在linux或windows平台上使用pyscreenshot。我用的是Ubuntu,它对我有用。如果应用了子流程,则可以强制将其设置为false,同时使用mss可以提供最佳性能

import pyscreenshot as ImageGrab
import time
t1 = time.time()
imgScreen = ImageGrab.grab(backend="mss", childprocess=False)
img = imgScreen.resize((640,480))
img.save("screen.png")
t2 = time.time()
print("The passing time",(t2-t1))


im=ImageGrab.grab(bbox=(10,10500500))这应该标记为正确答案。另外,它在Windows上工作得最好,难道不
ImageGrab.grab()
缩放图像吗?在您的示例中,图像的大小是否为490x490像素?很遗憾,没有。im.save()就足够了