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:
bbox(Optional): A 4-tuple(x1, y1, x2, y2), defining the screenshot area. By default, captures the entire screen.
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:
- If there is an image in the clipboard, return
PIL.Imageobject. - Otherwise returns
None。
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
:
(
"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
-
Cross-platform support:
系统设置 > 隐私与安全性 > 屏幕录制 terminal or IDE in ).X11 or Wayland, may need to install scrot or maim and other tools.-
Performance optimization:
-
Image processing:
ImageFilter) for further processing.I hope this tutorial can help you quickly master ImageGrab the use of the module!