Python mss Examples
mss(https://github.com/BoboTiG/python-mss) is a Python library that allows you to take screenshots using various methods and access screen pixel data. In this tutorial, we will explore various examples of how to use the mss library in Python.
To get started, you'll need to install the mss library. You can do this by running the following command:
pip install mss
Once the installation is complete, you can import the library in your Python script:
import mss
Now let's dive into some examples:
Example 1: Taking a Screenshot
To take a screenshot using mss, you can use the mss.mss() function to create a screenshot object, and then use the shot() method to capture the screen. Here's an example:
import mss
with mss.mss() as sct:
screenshot = sct.shot()
print("Screenshot taken!")
Expected Output:
Screenshot taken!
Example 2: Taking a Screenshot of a Specific Monitor
If you have multiple monitors and you want to take a screenshot of a specific monitor, you can use the mon parameter of the shot() method. The mon parameter specifies the index of the monitor you want to capture. Here's an example:
import mss
with mss.mss() as sct:
monitor_index = 1 # Index of the monitor you want to capture
screenshot = sct.shot(mon=monitor_index)
print("Screenshot taken!")
Expected Output:
Screenshot taken!
Example 3: Capturing a Specific Region of the Screen
If you only want to capture a specific region of the screen, you can use the region parameter of the shot() method. The region parameter specifies the coordinates of the top-left and bottom-right corners of the region you want to capture. Here's an example:
import mss
with mss.mss() as sct:
top = 100 # Top coordinate of the region
left = 100 # Left coordinate of the region
width = 500 # Width of the region
height = 300 # Height of the region
screenshot = sct.shot(mon=0, region=(left, top, left + width, top + height))
print("Screenshot taken!")
Expected Output:
Screenshot taken!
Example 4: Accessing Pixel Data
mss also allows you to access the pixel data of a screenshot. You can use the grab() method to capture a screenshot and then access the pixel data using the pixels attribute of the screenshot object. Here's an example:
import mss
with mss.mss() as sct:
screenshot = sct.grab()
# Accessing pixel data
pixels = screenshot.pixels
# Printing pixel data of the first pixel
print(pixels[0])
Expected Output:
(255, 255, 255, 255)
Example 5: Capturing Multiple Screenshots
You can capture multiple screenshots by using a loop. Here's an example that captures 5 screenshots:
import mss
with mss.mss() as sct:
for i in range(5):
screenshot = sct.shot()
print(f"Screenshot {i+1} taken!")
Expected Output:
Screenshot 1 taken!
Screenshot 2 taken!
Screenshot 3 taken!
Screenshot 4 taken!
Screenshot 5 taken!
Example 6: Saving Screenshots to File
mss allows you to save the screenshots directly to a file. You can use the save() method of the screenshot object to save the image to a file. Here's an example:
import mss
with mss.mss() as sct:
screenshot = sct.shot()
screenshot.save("screenshot.png")
print("Screenshot saved!")
Expected Output:
Screenshot saved!
Example 7: Capturing Screenshots at Regular Intervals
If you want to capture screenshots at regular intervals, you can use the time.sleep() function to introduce a delay between each capture. Here's an example that captures a screenshot every 5 seconds:
import mss
import time
with mss.mss() as sct:
for i in range(5):
screenshot = sct.shot()
print(f"Screenshot {i+1} taken!")
time.sleep(5)
Expected Output:
Screenshot 1 taken!
Screenshot 2 taken!
Screenshot 3 taken!
Screenshot 4 taken!
Screenshot 5 taken!
Example 8: Specifying a Different Output Format
By default, mss saves the screenshots in PNG format. However, you can specify a different output format by providing the output parameter to the shot() method. Here's an example that saves the screenshot in JPEG format:
import mss
with mss.mss() as sct:
screenshot = sct.shot(output="screenshot.jpg")
print("Screenshot saved!")
Expected Output:
Screenshot saved!
Example 9: Getting Information about Monitors
mss provides a monitors attribute that allows you to get information about the available monitors. Here's an example:
import mss
with mss.mss() as sct:
monitors = sct.monitors
# Printing information about each monitor
for i, monitor in enumerate(monitors):
print(f"Monitor {i+1}:")
print(f" Top: {monitor['top']}")
print(f" Left: {monitor['left']}")
print(f" Width: {monitor['width']}")
print(f" Height: {monitor['height']}")
Expected Output:
Monitor 1:
Top: 0
Left: 0
Width: 1920
Height: 1080
Monitor 2:
Top: -1080
Left: 0
Width: 1920
Height: 1080
Example 10: Capturing Cursor in Screenshot
If you want to capture the cursor along with the screenshot, you can use the with_cursor parameter of the shot() method. Set with_cursor to True to include the cursor in the screenshot. Here's an example:
import mss
with mss.mss() as sct:
screenshot = sct.shot(with_cursor=True)
print("Screenshot taken with cursor!")
Expected Output:
Screenshot taken with cursor!
These examples should give you a good starting point for using the mss library in Python. You can explore the library further to see additional features and functionality it provides.