Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking Screen shots of specific size

Tags:

python

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

like image 313
Belrouk Avatar asked Oct 31 '13 02:10

Belrouk


2 Answers

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')
like image 122
Sagar Rakshe Avatar answered Oct 01 '22 19:10

Sagar Rakshe


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.

like image 38
abarnert Avatar answered Oct 01 '22 18:10

abarnert