In this article, let us see how to use Python to capture a specific portion of a screen. In many circumstances, we need to take a screenshot of a specific portion of the application.
Taking Screenshots of Particular Size in Python
Below are the ways to Take Screenshots of Particular Size in Python:
Method #1: Using pyscreenshot Module
Before we work with this pyscreenshot module we use should first install pillow (PIL) on our system as shown below.
Installation:
pip install pillow
pip install pyscreenshot
Output:
Collecting pyscreenshot Downloading pyscreenshot-3.0-py3-none-any.whl (27 kB) Collecting jeepney Downloading jeepney-0.7.1-py3-none-any.whl (54 kB) |████████████████████████████████| 54 kB 1.9 MB/s Collecting EasyProcess Downloading EasyProcess-1.1-py3-none-any.whl (8.7 kB) Collecting mss Downloading mss-6.1.0-py3-none-any.whl (76 kB) |████████████████████████████████| 76 kB 4.0 MB/s Collecting entrypoint2 Downloading entrypoint2-1.0-py3-none-any.whl (9.8 kB) Installing collected packages: mss, jeepney, entrypoint2, EasyProcess, pyscreenshot Successfully installed EasyProcess-1.1 entrypoint2-1.0 jeepney-0.7.1 mss-6.1.0 pyscreenshot-3.0
Approach:
- Import pyscreenshot module using the import keyword
- Pass the dimensions of the screen in pixels to the bbox() function and pass it as an argument to the grab()function to capture the screenshot of the given size.
- Store it in a variable.
- Display the screenshot using the show() function
- Save the screenshot with some random name in .png format using the save() function.
- The Exit of the Program.
Below is the implementation:
# Import pyscreenshot module using the import keyword import pyscreenshot # Pass the dimensions of the screen in pixels to the bbox() function and # pass it as an argument to the grab() to capture the screenshot of the given size. # Store it in a variable. screenshot = pyscreenshot.grab(bbox=(100, 200, 450, 350)) # Display the screenshot using the show() function screenshot.show() # Save the screenshot with some random name in .png format using the save() function screenshot.save("sample_screenshot.png")
Output:
Method #2: Using pillow Module
Approach:
- Import ImageGrab from PIL(pillow) module using the import keyword
- Pass the dimensions of the screen in pixels to the bbox() function and pass it as an argument to the grab() function of the ImageGrab to capture the screenshot of the given size.
- Store it in a variable.
- Display the screenshot using the show() function
- Save the screenshot with some random name in .png format using the save() function.
- The Exit of the Program.
Below is the implementation:
# Import ImageGrab from PIL(pillow) module using the import keyword from PIL import ImageGrab # Pass the dimensions of the screen in pixels to the bbox() function and # pass it as an argument to the grab() function of the ImageGrab to capture the # screenshot of the given size. # Store it in a variable. screenshot = ImageGrab.grab(bbox=(90, 120, 450, 350)) # Display the screenshot using the show() function screenshot.show() # Save the screenshot with some random name in .png format using the save() function screenshot.save("output_screenshot.png")
Output: