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.

Similar Reads

Method 1: Using pyautogui.screenshot()

pyautogui: The pyautogui library provides the screenshot() method, The screenshot() method is used to take a screenshot. We will use the random library and sleep() method to take screenshots at Random Intervals....

Method 2: Using ImageGrab.grab()

...

Method 3: Using shot()  from MSS module

The pillow (PIL) library has an ImageGrab module that can be used to copy the contents of the screen or the clipboard to a PIL image memory....