How to use shot() from MSS module In Python
MSS is an ultra-fast cross-platform multiple screenshots module in pure python using ctypes. This module can be used to capture screenshots.
Installing MSS
pip install mss
The shot() method is used to take a screenshot of the screen or monitor.
Syntax: mss_instance.shot(“file_destination”)
Parameters:
- file_destination: The place where screenshots need to be saved.
Example:
In the above code, a random delay was generated between 1 to 5 seconds before taking a screenshot. generate a random number between 1 to 5, and create a time delay using the shot() method. The screenshots will be saved in the exact location of the program with the file name as the current time.
- Import PIL, random, and time libraries.
- Create an instance of mss using with keyword, Take a screenshot using the shot() method and wait for sometime
- Create the random delay using randint() method and sleep().
- save the screenshots with the name as the current time.
Python3
# importing random module import random import mss # importing time module import time # Running the while loop for infinite time while True : # generating a random number between 1 # to 5 , which will represent the time # delay random_time = random.randint( 1 , 5 ) # create a time delay using the sleep() # method time.sleep(random_time) # Save the screenshot shot using current time # as file name. file_name = str (time.time()) + ".png" # Take the screenshot using shot() method with mss.mss() as mss_obj: mss_obj.shot(output = file_name) |
Output:
Take Screenshots at Random Intervals with Python
In this article, we will learn how to take screenshots at Random Intervals with Python.