Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 40 additions & 28 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,14 @@ list of all the monitors, starting from index 1, as well as the full virtual scr
The primary monitor, the one that holds the taskbar or similar system UI, is available as
:py:attr:`mss.MSS.primary_monitor`.

Each entry is an immutable :py:class:`mss.models.Monitor`. Its geometry is available through ``left``, ``top``,
``width``, and ``height`` attributes. The ``is_primary``, ``name``, ``unique_id``, and ``output`` metadata attributes
are ``None`` when unavailable::
For capturing a specific region, you can describe the area with a :py:class:`mss.models.Region`, a dictionary with
``left``, ``top``, ``width``, and ``height`` keys, or a PIL-style 4-tuple ``(left, top, right, bottom)``. See
:ref:`monitors_and_regions` and :py:mod:`mss.models` for details on ``Monitor`` and ``Region`` objects.

monitor = sct.primary_monitor
print(monitor.width, monitor.height)

Call ``monitor.as_region()`` when you need its geometry as a :py:class:`mss.models.Region`::

region = monitor.as_region()
region.width -= 1
screenshot = sct.grab(region)
Once you've decided what you want to capture, call :py:meth:`mss.MSS.grab` with the monitor or region. This returns a
:py:class:`mss.ScreenShot` object, which contains the pixel data and metadata for the screenshot.

For migration from MSS 10, string-key access such as ``monitor["width"]`` is temporarily supported. ``Monitor`` is not
a mapping, so dictionary methods, membership tests, and ``**monitor`` unpacking are not supported. New code should use
attributes.

For capturing a specific region, construct a mutable :py:class:`mss.models.Region` with ``top``, ``left``, ``width``,
and ``height`` attributes. Region dictionaries remain supported for compatibility::

screenshot = sct.grab({"top": 0, "left": 0, "width": 100, "height": 100})

You can also pass :py:meth:`mss.MSS.grab` a PIL-style box, which is a 4-tuple of
``(left, top, right, bottom)``.

Once you've decided what you want to capture, you can call :py:meth:`mss.MSS.grab` with the appropriate monitor or
region. This will return a :py:class:`mss.ScreenShot` object, which contains the pixel data and other information about
the screenshot.

For instance, you can capture the first monitor and get a :py:class:`mss.ScreenShot` object like this::
For instance, you can capture the primary monitor and get a :py:class:`mss.ScreenShot` object like this::

with MSS() as sct:
sct_img = sct.grab(sct.primary_monitor)
Expand Down Expand Up @@ -192,6 +170,40 @@ If you want to ensure that memory is not shared, you can make a copy of the retu
want to ensure that a NumPy array does not share memory with the original :py:class:`mss.ScreenShot` object, you can
use the :py:meth:`numpy.ndarray.copy` method to create a copy of the array.

.. _monitors_and_regions:

Monitors and Regions
====================

Each entry in :py:attr:`mss.MSS.monitors` (and :py:attr:`mss.MSS.primary_monitor`) is an immutable
:py:class:`mss.models.Monitor`. Its geometry is available through ``left``, ``top``, ``width``, and ``height``
attributes. The ``is_primary``, ``name``, ``unique_id``, and ``output`` metadata attributes are ``None`` when
unavailable::

monitor = sct.primary_monitor
print(monitor.width, monitor.height)

When you need a mutable capture area based on a monitor, call :py:meth:`mss.models.Monitor.as_region` to obtain a
:py:class:`mss.models.Region`::

region = monitor.as_region()
region.width -= 1
screenshot = sct.grab(region)

You can also construct a :py:class:`mss.models.Region` directly with ``left``, ``top``, ``width``, and ``height``
attributes, pass a dictionary with those keys, or pass a PIL-style 4-tuple ``(left, top, right, bottom)`` to
:py:meth:`mss.MSS.grab`::

from mss.models import Region

screenshot = sct.grab(Region(left=0, top=0, width=100, height=100))
screenshot = sct.grab({"left": 0, "top": 0, "width": 100, "height": 100})
screenshot = sct.grab((0, 0, 100, 100))

For migration from MSS 10, string-key access such as ``monitor["width"]`` is temporarily supported. ``Monitor`` does
not implement mapping methods, membership tests, or ``**monitor`` unpacking. New code should access attributes
directly. See :py:mod:`mss.models` for the complete API reference.

Intensive Use
=============

Expand Down