Taking Screen shots of specific size

2020-02-29 03:00发布

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
4条回答
▲ chillily
2楼-- · 2020-02-29 03:33

You can use pyscreenshot module.
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.

$ 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')
查看更多
做个烂人
3楼-- · 2020-02-29 03:33

I have tried PIL, but can't seem to make ImageGrab.grab() select a small rectangle

What did you try?

As the documentation for ImageGrab clearly states, the function has a bbox parameter, and:

The pixels inside the bounding box are returned as an “RGB” image. If the bounding box is omitted, the entire screen is copied.

So, you only get the whole screen if you don't pass a 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:

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.

查看更多
聊天终结者
4楼-- · 2020-02-29 03:45

You could use 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)
查看更多
唯我独甜
5楼-- · 2020-02-29 03:57

1) Use pyscreenshot, ImageGrab works but only on Windows

2) Grab the image and box it, then save that image

3) Don't use ImageGrab.grab_to_file, it saves the full size image

4) You don't need to show the image with im.show if you just want to save a screenshot

import pyscreenshot as ImageGrab

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

im.save('im.png')
查看更多
登录 后发表回答