Python can allow us to take screenshot easily. In this tutorial, we will introduce using python pillow library to take a screenshot for python beginners.
from PIL import ImageGrab
We will use ImageGrab model to take a screenshot.
Create a region you want to take
bbox = (300, 300, 550, 550)
In this tutorial, we will take a image 250 * 250.
In order to set bbox correctly, you can read this tutorial.
im = ImageGrab.grab(bbox)
We will use ImageGrab.grab() function to get a snapshot of the screen.
Save snapshot to a file
After getting a screenshot, we will save this image into a file. Open this file, we will see the snapshot is:
Meanwhile, if you want to get a snapshot of full screen, you have two methods:
1.Set bbox = None, here is an example:
bbox = None im = ImageGrab.grab(bbox)
Then you will get full screenshot of computer.
2.Get screen width and height
How to get screen width and height, you can read this tutorial.
Then you can do like this:
bbox = (0, 0, screen_widht, screen_height) im = ImageGrab.grab(bbox)