There are numerous approaches to capture the screenshots in Python.
Python includes a number of libraries for capturing screenshots. In this article, let us look at a handful of these libraries and their implementation using Python code.
1)By Using pyautogui module
The pyautogui module employs the screenshot function, which is in charge of capturing a snapshot of the entire computer screen. The save function is then utilized to save the screenshot to our device.
2) By using pillow Module
An ImageGrab submodule is used by the pillow module. This method necessitates the capture of a region, which necessitates the setting of the region’s diagonal coordinates.
Then we use the grab function, which takes the region parameters and uses them to capture the screenshot. Finally, use the save function to save the taken image.
Before going to the coding part, install the pyautogui module in your system as shown below:
pip install pyautogui
Output:
Collecting pyautogui Downloading PyAutoGUI-0.9.53.tar.gz (59 kB) |████████████████████████████████| 59 kB 3.7 MB/s Collecting pymsgbox Downloading PyMsgBox-1.0.9.tar.gz (18 kB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done Collecting PyTweening>=1.0.1 Downloading pytweening-1.0.4.tar.gz (14 kB) Collecting pyscreeze>=0.1.21 Downloading PyScreeze-0.1.28.tar.gz (25 kB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done Collecting pygetwindow>=0.0.5 Downloading PyGetWindow-0.0.9.tar.gz (9.7 kB) Collecting mouseinfo Downloading MouseInfo-0.1 .3.tar.gz (10 kB) Collecting python3-Xlib Downloading python3-xlib-0.15.tar.gz (132 kB) |████████████████████████████████| 132 kB 18.8 MB/s Collecting pyrect Downloading PyRect-0.1.4.tar.gz (15 kB) Requirement already satisfied: Pillow>=5 .2.0 in /usr/local/lib/python3.7/dist-packages (from pyscreeze>=0.1.21-> pyautogui) (7.1.2) Collecting pyperclipBuilding wheel for pyrect (setup.py) ... done Created wheel for pyrect: filename=PyRect-0.1.4-py2.py3-none-any.whl size=95 47 sha256=ed4a10cef4885f276a3067e64ab033e9507adea168952f5ce4636cb3a6f6d12e Stored in directory: /root/.cache/pip/wheels/97/5f/8e/6f26a5b00d46679ee2391a3542 334274ce8bdaf7c6b0f3504c Building wheel for python3-Xlib (setup.py) ... done Created wheel for python3-Xlib: filename=python3_xlib-0.15-py3-none-any.whl size=109517 sha256=9844de06645f2fc25ed8ae389ad41da84bbe8ed102c6013ce3b88c10c 5e216df Stored in directory: /root/.cache/pip/wheels/67/6f/f2/18f51230840318 e784c45e1392a0e174777e499251e42ddf86 Successfully built pyautogui pygetwindow pyscreeze PyTweening mouseinfo pymsgbox pyperclip pyrect python3-Xlib Installing collected packages: python3-Xlib, pyrect, pyperclip, PyTweening, pyscreeze, pymsgbox, pygetwindow, mouseinfo, pyautogui Successfully installed PyTweening-1.0.4 mouseinfo-0.1.3 pyautogui-0.9.53 pygetwindow-0.0.9 pymsgbox-1 .0.9 pyperclip-1.8.2 pyrect-0.1.4 pyscreeze-0.1.28 python3-Xlib-0.15
Method #1: Using Built-in Functions (Static Input)
1)By Using pyautogui module
Approach:
- Import pyautogui module using the import keyword.
- Take the screenshot using the pyautogui.screenshot() function and store it in a variable
- Save the above screenshot using the save() function
- The Exit of the Program.
Below is the implementation:
# Import pyautogui module using the import keyword. import pyautogui # Take the screenshot using the pyautogui.screenshot() function and # store it in a variable scrnsht = pyautogui.screenshot() # Save the above screenshot using the save() function scrnsht.save("SS1.jpg")
2) By using pillow Module
Approach:
- Import ImageGrab function from PIL module using the import keyword.
- Give the diagonal coordinates as static input and store it in a variable.
- Pass the above given diagonal coordinates to the ImageGrab.grab() function to take a screenshot and store it in another variable.
- Save the above screenshot using the save() function.
- The Exit of the Program.
Below is the implementation:
# Import ImageGrab function from PIL module using the import keyword. from PIL import ImageGrab # Give the diagonal coordinates as static input and store it in a variable diagnl_cordnates = (200, 200, 400, 400) # Pass the above given diagonal coordinates to the ImageGrab.grab() function # to take a screenshot and store it in another variable scrnsht = ImageGrab.grab(diagnl_cordnates) # Save the above screenshot using the save() function. scrnsht.save("myscreenshot.jpg")