python-mss
An ultra fast cross-platform multiple screenshots module in pure Python using ctypes.
Top Related Projects
Go library to capture desktop to image
Sends virtual input commands
A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
Hook and simulate global keyboard events on Windows and Linux.
Quick Overview
Python-mss is a fast cross-platform multiple screenshots module in pure Python using ctypes. It allows you to take screenshots of single or multiple monitors efficiently, supporting various operating systems including Windows, macOS, and Linux (X11 or Wayland).
Pros
- Fast performance due to its use of ctypes and low-level APIs
- Cross-platform compatibility (Windows, macOS, Linux)
- Simple and easy-to-use API
- Supports capturing multiple monitors
Cons
- Limited image processing capabilities (focused mainly on capturing screenshots)
- Requires additional dependencies for saving images in formats other than PNG
- May have compatibility issues with some less common display setups or configurations
- Documentation could be more comprehensive for advanced use cases
Code Examples
Capturing a full screen screenshot:
from mss import mss
with mss() as sct:
filename = sct.shot(output="fullscreen.png")
print(f"Screenshot saved to: {filename}")
Capturing a specific monitor:
from mss import mss
with mss() as sct:
monitor = sct.monitors[1] # Get the second monitor
screenshot = sct.grab(monitor)
mss.tools.to_png(screenshot.rgb, screenshot.size, output="monitor2.png")
Capturing a custom region:
from mss import mss
with mss() as sct:
region = {"top": 100, "left": 200, "width": 300, "height": 400}
screenshot = sct.grab(region)
mss.tools.to_png(screenshot.rgb, screenshot.size, output="custom_region.png")
Getting Started
To get started with python-mss, follow these steps:
-
Install the library using pip:
pip install mss
-
Import the library in your Python script:
from mss import mss
-
Use the
mss()
context manager to create a screenshot object and capture screenshots:with mss() as sct: # Capture full screen sct.shot() # Capture specific monitor monitor = sct.monitors[0] # First monitor sct.shot(mon=monitor, output="monitor1.png")
For more advanced usage and options, refer to the project's documentation on GitHub.
Competitor Comparisons
Go library to capture desktop to image
Pros of screenshot
- Cross-platform support (Windows, macOS, Linux)
- Lightweight and focused solely on screenshot functionality
- Simple API for capturing screenshots
Cons of screenshot
- Less actively maintained compared to python-mss
- Fewer advanced features and customization options
- Limited documentation and examples
Code Comparison
python-mss:
from mss import mss
with mss() as sct:
sct.shot()
screenshot:
#include <screenshot.h>
screenshot::Screenshot ss;
ss.capture();
ss.save("screenshot.png");
Key Differences
- python-mss is Python-based, while screenshot is C++-based
- python-mss offers more advanced features like multi-monitor support and custom capture regions
- screenshot provides a simpler API for basic screenshot functionality
- python-mss has more extensive documentation and community support
- screenshot may have better performance in certain scenarios due to its C++ implementation
Use Cases
- Choose python-mss for Python projects requiring advanced screenshot capabilities
- Opt for screenshot in C++ projects or when a lightweight, cross-platform solution is needed
Both libraries serve their purpose well, but python-mss is generally more feature-rich and actively maintained, making it a better choice for most Python-based screenshot applications.
Sends virtual input commands
Pros of pynput
- Provides comprehensive input device control (keyboard and mouse)
- Offers cross-platform support (Windows, macOS, Linux)
- Includes both input monitoring and control capabilities
Cons of pynput
- May require additional setup for certain platforms
- Limited to input device functionality, not screen capture
- Potential performance overhead for complex input operations
Code Comparison
pynput example (keyboard monitoring):
from pynput import keyboard
def on_press(key):
print(f'Key {key} pressed')
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
python-mss example (screen capture):
import mss
with mss.mss() as sct:
monitor = sct.monitors[0]
screenshot = sct.grab(monitor)
mss.tools.to_png(screenshot.rgb, screenshot.size, output="screenshot.png")
Summary
pynput focuses on input device control and monitoring, offering cross-platform support for keyboard and mouse interactions. It's ideal for applications requiring input manipulation or tracking. python-mss, on the other hand, specializes in fast screen capture functionality. While pynput provides more comprehensive input control, it lacks screen capture capabilities. The choice between the two depends on whether the project requires input device management or screen capture functionality.
A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
Pros of pyautogui
- More comprehensive GUI automation features, including mouse and keyboard control
- Extensive documentation and tutorials available
- Cross-platform support for Windows, macOS, and Linux
Cons of pyautogui
- Slower screen capture performance compared to python-mss
- Less focused on screen capture functionality
- Larger library size due to broader feature set
Code Comparison
python-mss (screen capture):
from mss import mss
with mss() as sct:
screenshot = sct.grab(sct.monitors[0])
mss.tools.to_png(screenshot.rgb, screenshot.size, output="screenshot.png")
pyautogui (screen capture):
import pyautogui
screenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")
python-mss focuses solely on efficient screen capture, while pyautogui offers a broader range of GUI automation tools. python-mss provides faster screen capture performance, especially for continuous capturing. pyautogui, on the other hand, offers more versatility with its additional features for mouse and keyboard control.
The code comparison demonstrates the simplicity of both libraries for basic screen capture tasks. python-mss requires slightly more setup but offers more control over the capture process, while pyautogui provides a more straightforward one-line solution for quick screenshots.
Hook and simulate global keyboard events on Windows and Linux.
Pros of keyboard
- Focuses specifically on keyboard input, offering more advanced keyboard-related features
- Provides cross-platform support for Windows, Linux, and macOS
- Includes both global and non-global hotkeys functionality
Cons of keyboard
- Limited to keyboard input, unlike python-mss which handles screen capture
- May require additional dependencies for certain features on some platforms
- Less actively maintained compared to python-mss (fewer recent updates)
Code Comparison
keyboard:
import keyboard
keyboard.add_hotkey('ctrl+shift+a', print, args=('triggered', 'hotkey'))
keyboard.wait('esc')
python-mss:
import mss
with mss.mss() as sct:
sct.shot()
The code snippets demonstrate the core functionality of each library. keyboard focuses on keyboard event handling, while python-mss is designed for screen capture operations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Python MSS
from mss import mss
# The simplest use, save a screenshot of the 1st monitor
with mss() as sct:
sct.shot()
An ultra-fast cross-platform multiple screenshots module in pure python using ctypes.
- Python 3.9+, PEP8 compliant, no dependency, thread-safe;
- very basic, it will grab one screenshot by monitor or a screenshot of all monitors and save it to a PNG file;
- but you can use PIL and benefit from all its formats (or add yours directly);
- integrate well with Numpy and OpenCV;
- it could be easily embedded into games and other software which require fast and platform optimized methods to grab screenshots (like AI, Computer Vision);
- get the source code on GitHub;
- learn with a bunch of examples;
- you can report a bug;
- need some help? Use the tag python-mss on Stack Overflow;
- and there is a complete, and beautiful, documentation :)
- MSS stands for Multiple ScreenShots;
Installation
You can install it with pip:
python -m pip install -U --user mss
Or you can install it with Conda:
conda install -c conda-forge python-mss
Top Related Projects
Go library to capture desktop to image
Sends virtual input commands
A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
Hook and simulate global keyboard events on Windows and Linux.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot