Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions docs/source/release-history/v11.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ The {py:attr}`mss.ScreenShot.bgra` and {py:attr}`mss.ScreenShot.rgb` properties
{py:class}`memoryview` objects, rather than {py:class}`bytes` or {py:class}`bytearray` objects. For practical use
cases, this should not be noticeable. This change allows faster access to screenshot data, with fewer memory copies.

The first access to {py:attr}`mss.ScreenShot.rgb` now converts the pixels in small batches, speeding up the conversion
and reducing temporary memory use for large screenshots.

#### Immutable monitor objects

{py:attr}`mss.MSS.monitors` now returns immutable {py:class}`mss.models.Monitor`
Expand Down
4 changes: 4 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ any of several methods:
* :py:attr:`mss.ScreenShot.pixels`: A 2d array (list of lists) of ``(R, G, B)`` tuples.
* :py:meth:`mss.ScreenShot.pixel`: Access ``(R, G, B)`` tuples of a particular x, y coordinate.

The first access to :py:attr:`mss.ScreenShot.rgb` converts pixels in chunks of 8,192. You can edit the
``_RGB_CONVERSION_CHUNK_PIXELS`` constant in ``src/mss/screenshot.py`` to benchmark different chunk sizes in a
local checkout. Smaller chunks use less temporary memory, and conversion time varies with the chunk size.

Often, though, you'll export screenshot data to a different framework. You can often do this by passing the
:py:attr:`mss.ScreenShot.bgra` data to the framework's appropriate function. MSS also provides easy-to-use methods to
work with many popular frameworks:
Expand Down
18 changes: 14 additions & 4 deletions src/mss/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
Channels = Literal["BGRA", "BGR", "RGB", "RGBA"]
Layout = Literal["HWC", "CHW"]

_RGB_CONVERSION_CHUNK_PIXELS = 8_192


class ScreenShot:
"""Screenshot object.
Expand Down Expand Up @@ -185,11 +187,19 @@ def rgb(self) -> memoryview:
:py:meth:`memoryview.tobytes` to get a ``bytes`` object.
"""
if self.__rgb is None:
rgb = bytearray(self.height * self.width * 3)
pixel_count = self.height * self.width
rgb = bytearray(pixel_count * 3)
raw = self._raw
rgb[::3] = raw[2::4]
rgb[1::3] = raw[1::4]
rgb[2::3] = raw[::4]
# Slice contiguous bytes instead of strided memoryviews. Copying in
# chunks keeps the temporary buffers small for large screenshots.
pixels_per_chunk = _RGB_CONVERSION_CHUNK_PIXELS
for first_pixel in range(0, pixel_count, pixels_per_chunk):
last_pixel = min(first_pixel + pixels_per_chunk, pixel_count)
bgra = raw[first_pixel * 4 : last_pixel * 4].tobytes()
start, end = first_pixel * 3, last_pixel * 3
rgb[start:end:3] = bgra[2::4]
rgb[start + 1 : end : 3] = bgra[1::4]
rgb[start + 2 : end : 3] = bgra[::4]
# We could just return the bytearray directly. However, we would rather give ourselves the flexibility to
# use other buffer types in the future. Rather than declaring our return type as just generically Buffer
# (which doesn't guarantee the indexing behavior of memoryview), we always return a memoryview, which is
Expand Down
9 changes: 9 additions & 0 deletions src/tests/test_bgra_to_rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ def test_contents() -> None:
assert bytes(image.rgb) == b"RGB" * 1024 * 768


def test_contents_across_chunks() -> None:
width, height = 513, 49
repeats = width * height // 3
data = memoryview(b"_" + b"abcdefghijkl" * repeats)[1:]
image = ScreenShot.from_size(data, width, height)

assert bytes(image.rgb) == b"cbagfekji" * repeats


def test_ctypes_pointers_from_rgb() -> None:
image = ScreenShot.from_size(b"BGRA" * 4, 2, 2)
assert image.rgb.readonly is False
Expand Down