A screenshot is nothing but an image of the data displayed on the screen of a mobile or computer device whereas a photograph is an image captured using the camera of a mobile device or a computer. The screenshot is also termed as screen capture or screengrab.
In this case, we will be using "pyautogui" module, which controls mouse and keyboard actions to take screenshots using python.
Installation
To install pyautogui module, use
pip install pyautogui
To verify whether the module is installed or not, use the command as
pip list
On specifying this command, there comes a list of modules installed on your device, where one can crosscheck whether the required module is installed on your device.
To view built-in functions of pyautogui module, use
dir(pyautogui)
This command lists out some of the built-in functions present inside this particular module.
The screenshot( ) in pyautogui module simply returns an Image file that was captured as a screenshot on a device.
Syntax:
import pyautogui
image = pyautogui.screenshot( )
or
image = pyautogui.screenshot("filename.extension")
where the filename of the image was passed as an argument and specifying the extension of the image file is mandatory.
In some cases, we don't need to capture screenshots of the entire screen, only a specific portion of the screen is required, for that use,
image = pyautogui.screenshot(region=(0 , 0 , 100 , 300))
save( )
The save( ) function in pyautogui module, saves the image file in the specified location. The path to save the image file is passed as an argument inside the save( ) function.
Example
image.save(r"D:\filename.jpg")
where the filename specified in the argument is the filename of the image to be stored in the destined location.
Code Snippet
import pyautogui
img = pyautogui.screenshot( )
img.save(r"D:\pythonscreenshot.jpg")
Comments
Post a Comment