From 68fd94fdf3927a0d6efc62ddfeb66c5821b54914 Mon Sep 17 00:00:00 2001 From: Wimpykid1a <113426421+Wimpykid1a@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:17:24 +0800 Subject: [PATCH 1/2] perf: convert BGRA pixels to RGB in bounded chunks --- docs/source/release-history/v11.0.0.md | 3 +++ src/mss/screenshot.py | 16 ++++++++++++---- src/tests/test_bgra_to_rgb.py | 9 +++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/docs/source/release-history/v11.0.0.md b/docs/source/release-history/v11.0.0.md index b45792cb..ce6fa48f 100644 --- a/docs/source/release-history/v11.0.0.md +++ b/docs/source/release-history/v11.0.0.md @@ -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` diff --git a/src/mss/screenshot.py b/src/mss/screenshot.py index 3fa4c69e..a1d433d9 100644 --- a/src/mss/screenshot.py +++ b/src/mss/screenshot.py @@ -185,11 +185,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 = 8192 + 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 diff --git a/src/tests/test_bgra_to_rgb.py b/src/tests/test_bgra_to_rgb.py index 6cd46816..9dced7cd 100644 --- a/src/tests/test_bgra_to_rgb.py +++ b/src/tests/test_bgra_to_rgb.py @@ -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 From b58c998f2dfe1a3cecea679097afae42e2adb949 Mon Sep 17 00:00:00 2001 From: Wimpykid1a <113426421+Wimpykid1a@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:38:21 +0800 Subject: [PATCH 2/2] docs: make the RGB conversion chunk size easier to benchmark --- docs/source/usage.rst | 4 ++++ src/mss/screenshot.py | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 04af339a..9a05075b 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -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: diff --git a/src/mss/screenshot.py b/src/mss/screenshot.py index a1d433d9..ae5bb2c8 100644 --- a/src/mss/screenshot.py +++ b/src/mss/screenshot.py @@ -24,6 +24,8 @@ Channels = Literal["BGRA", "BGR", "RGB", "RGBA"] Layout = Literal["HWC", "CHW"] +_RGB_CONVERSION_CHUNK_PIXELS = 8_192 + class ScreenShot: """Screenshot object. @@ -190,7 +192,7 @@ def rgb(self) -> memoryview: raw = self._raw # Slice contiguous bytes instead of strided memoryviews. Copying in # chunks keeps the temporary buffers small for large screenshots. - pixels_per_chunk = 8192 + 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()