Skip to content

Commit c27a14e

Browse files
authored
fix: Pillow examples using frombuffer (#535)
* Fix Pillow examples using frombuffer The Pillow docs recommend always using the decoder arguments when using frombuffer; see https://pillow.readthedocs.io/en/stable/reference/Image.html * Add PR number to release notes * Remove a release notes change that was put in this PR by mistake
1 parent 110e297 commit c27a14e

7 files changed

Lines changed: 14 additions & 7 deletions

File tree

demos/cat-detector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def main() -> None:
281281
# We transfer the image from MSS to PyTorch via a Pillow Image. Faster approaches exist (see
282282
# screenshot_to_tensor), but PIL is more readable. The bulk of the time in this program is spent doing
283283
# the AI work, so we just use the most convenient mechanism.
284-
img = Image.frombuffer("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
284+
img = Image.frombuffer("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX", 0, 1)
285285

286286
# We explicitly convert it to a tensor here, even though Torchvision can also convert it in the preprocess
287287
# step. This is so that we send it to the GPU before we do the preprocessing: PIL Images are always on

demos/tinytv-stream-simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def main() -> None:
9797
# The next step is to resize the image to fit the TinyTV's screen. There's a great image
9898
# manipulation library called PIL, or Pillow, that can do that. Let's transfer the raw pixels in
9999
# the ScreenShot object into a PIL Image.
100-
original_image = Image.frombuffer("RGB", screenshot.size, screenshot.bgra, "raw", "BGRX")
100+
original_image = Image.frombuffer("RGB", screenshot.size, screenshot.bgra, "raw", "BGRX", 0, 1)
101101

102102
# Now, we can resize it. The resize method may stretch the image to make it match the TinyTV's
103103
# screen; the advanced demo gives other options. Using a reducing gap is optional, but speeds up

demos/tinytv-stream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def capture_image(
348348

349349
while True:
350350
sct_img = sct.grab(rect)
351-
pil_img = Image.frombuffer("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
351+
pil_img = Image.frombuffer("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX", 0, 1)
352352
yield pil_img
353353

354354

demos/video-capture-simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def main() -> None:
129129
# use PIL: you can create an Image from the screenshot, and create a VideoFrame from that. That said,
130130
# if you want to boost the fps rate by about 50%, check out the full demo, and search for
131131
# from_numpy_buffer.
132-
img = Image.frombuffer("RGB", screenshot.size, screenshot.bgra, "raw", "BGRX")
132+
img = Image.frombuffer("RGB", screenshot.size, screenshot.bgra, "raw", "BGRX", 0, 1)
133133
frame = av.VideoFrame.from_image(img)
134134

135135
# When we encode frames, we get back a list of packets. Often, we'll get no packets at first: the

docs/source/examples/pil.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
sct_img = sct.grab(monitor)
1616

1717
# Create the Image
18-
img = Image.frombuffer("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
18+
img = Image.frombuffer("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX", 0, 1)
1919
# The same, but less efficient:
20-
# img = Image.frombuffer('RGB', sct_img.size, sct_img.rgb, 'raw', 'RGB')
20+
# img = Image.frombuffer('RGB', sct_img.size, sct_img.rgb, 'raw', 'RGB', 0, 1)
2121

2222
# And save it!
2323
output = f"monitor-{num}.png"

docs/source/release-history/v11.0.0.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ Device contexts are now acquired and released within each `grab()` call, allowin
3636
### General Improvements
3737

3838
The MSS context object will now always surface inner exceptions, even if `__exit__` may also generate an exception during tear-down.
39+
40+
### Documentation
41+
42+
The documentation has received numerous small improvements. A few highlights:
43+
44+
* The Pillow examples and demos using {py:meth}`PIL.Image.frombuffer` now explicitly specify the decoder arguments,
45+
as recommended by Pillow. (#535)

src/tests/third_party/test_pil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_pil_bgra(mss_impl: Callable[..., MSS]) -> None:
3737
with mss_impl() as sct:
3838
sct_img = sct.grab(box)
3939

40-
img = Image.frombuffer("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
40+
img = Image.frombuffer("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX", 0, 1)
4141
assert img.mode == "RGB"
4242
assert img.size == sct_img.size
4343

0 commit comments

Comments
 (0)