Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pillow ImageGrab Module

Pillow ImageGrab Module

ImageGrab allows users to capture the screen or a specified area of the image, save it as an image file, or perform further processing.

The ImageGrab module is suitable for automated testing, screen recording assistance, image recognition and other scenarios.

Import method:

from PIL import ImageGrab


ImageGrab core methods

The following table lists the main methods of the ImageGrab module and their descriptions:

Method Description Parameters Return Value
ImageGrab.grab() Capture the entire screen or a specified area of the image bbox(Optional): A tuple specifying the screenshot area (x1, y1, x2, y2), default full screen PIL.Image object
ImageGrab.grabclipboard() Get image from the system clipboard None PIL.Image Object (if the clipboard has an image), otherwise None
ImageGrab.grab_to_file() Capture the screen and save directly to a file (deprecated, recommended to use grab().save()) filename: File path to save None

Detailed method explanation

ImageGrab.grab()

Function:
Capture the screen or a specified area of the image.

Parameters:

Example Code:

Example

from

PIL

import

ImageGrab

Capture the entire screen

screenshot

=

ImageGrab.

grab

(

)

screenshot.

save

(

"full_screen.png"

)

Capture a specified area (top-left (100, 100) to bottom-right (500, 500))

partial_screenshot

=

ImageGrab.

grab

(

bbox

=

(

100

,

100

,

500

,

500

)

)

partial_screenshot.

save

(

"partial_screen.png"

)

ImageGrab.grabclipboard()

Function:
Get image data from the system clipboard (such as pressing after a screenshot Ctrl+C the copied image).

Return Value:

Example Code:

Example

from

PIL

import

ImageGrab

Get image from clipboard

clipboard_image

=

ImageGrab.

grabclipboard

(

)

if

clipboard_image:

    clipboard_image.

save

(

"clipboard_image.png"

)

else

:

   

print

(

"No image data in clipboard"

)

ImageGrab.grab_to_file()(Deprecated)

Note:
This method has been deprecated in newer versions of Pillow; it is recommended to use grab().save() as a replacement.

Example replacement code:

Example

from

PIL

import

ImageGrab

Alternative: grab() first, then save()

screenshot

=

ImageGrab.

grab

(

)

screenshot.

save

(

"screenshot.png"

)


Practical Application Examples

Scheduled screenshot tool

Example

import

time

from

PIL

import

ImageGrab

Take a screenshot every 5 seconds, 3 times total

for

i

in

range

(

3

)

:

    screenshot

=

ImageGrab.

grab

(

)

    screenshot.

save

(

f

"screenshot_{i+1}.png"

)

   

time

.

sleep

(

5

)

Combine with OpenCV for real-time screen analysis

Example

import

numpy

as

np

from

PIL

import

ImageGrab

import

cv2

Capture and display the screen in real time

while

True

:

    screenshot

=

ImageGrab.

grab

(

)

    img_np

=

np.

array

(

screenshot

)

 

Convert to a NumPy array

    cv2.

imshow

(

"Screen Capture"

,

cv2.

cvtColor

(

img_np

,

cv2.

COLOR_RGB2BGR

)

)

   

   

if

cv2.

waitKey

(

1

)

&

0xFF

==

ord

(

'q'

)

:  

Press 'q' to exit

       

break

cv2.

destroyAllWindows

(

)


Notes

  1. Cross-platform support:

  2. Windows: Fully supported.
  3. macOS: Requires additional permissions (enable in 系统设置 > 隐私与安全性 > 屏幕录制 terminal or IDE in ).
  4. Linux: Depends on X11 or Wayland, may need to install scrot or maim and other tools.
  5. - Windows: Fully supported. - macOS: Requires additional permissions (enable terminal or IDE in `System Settings > Privacy & Security > Screen Recording`). - Linux: Depends on `X11` or `Wayland`, may need to install `scrot` or `maim` and other tools.
    1. Performance optimization:

    2. Frequent screenshots may affect system performance; it is recommended to set a reasonable screenshot interval.
    3. - Frequent screenshots may affect system performance; it is recommended to set a reasonable screenshot interval.
      1. Image processing:

      2. Captured images can be directly processed with other Pillow modules (such as ImageFilter) for further processing.
      3. - Captured images can be directly processed with other Pillow modules (such as `ImageFilter`) for further processing.

        I hope this tutorial can help you quickly master ImageGrab the use of the module!