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
28 changes: 28 additions & 0 deletions Tests/test_image_frombytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,31 @@ def test_sanity(data_type: str) -> None:
im2 = Image.frombytes(im1.mode, im1.size, data)

assert_image_equal(im1, im2)


@pytest.mark.parametrize("mode", ("L", "RGBA"))
def test_readonly_buffer(mode: str) -> None:
size = (2, 2)
length = 4 * Image.getmodebands(mode)
data = bytes(bytearray(length))
im = Image.frombuffer(mode, size, data, "raw", mode, 0, 1)
assert im.readonly

replacement = b"\x01" * length
im.frombytes(replacement)

assert im.tobytes() == replacement
assert data == bytes(length)
assert not im.readonly


def test_mutable_core_with_readonly_image() -> None:
# Plugins may call frombytes() from load() before clearing the image's flag.
im = Image.new("L", (2, 2))
im.readonly = 1
core = im.im

im.frombytes(b"\x01" * 4)

assert im.im is core
assert im.tobytes() == b"\x01" * 4
22 changes: 22 additions & 0 deletions Tests/test_imagecms.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ def test_sanity() -> None:
hopper().point(t)


@pytest.mark.parametrize("in_place", (False, True))
def test_readonly_output(in_place: bool) -> None:
original = bytes([10, 20, 30, 255] * 4)
data = bytes(bytearray(original))
im = Image.frombuffer("RGBA", (2, 2), data, "raw", "RGBA", 0, 1)
assert im.readonly
profile = ImageCms.createProfile("sRGB")
transform = ImageCms.buildTransform(profile, profile, "RGBA", "RGBA")

if in_place:
expected = im.copy()
result = transform.apply_in_place(im)
else:
expected = Image.new("RGBA", im.size, (90, 80, 70, 255))
result = transform.apply(expected, im)

assert result is im
assert_image_equal(im, expected)
assert data == original
assert not im.readonly


def test_flags() -> None:
assert ImageCms.Flags.NONE.value == 0
assert ImageCms.Flags.GRIDPOINTS(0) == ImageCms.Flags.NONE
Expand Down
8 changes: 8 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ TODO
Other changes
=============

Copy read-only images before writing pixel data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:py:meth:`~PIL.Image.Image.frombytes` and :py:meth:`~PIL.ImageCms.ImageCmsTransform.apply`
now copy read-only image memory before writing to it. This also applies to in-place
ImageCms transforms, preventing writes into shared buffers or read-only memory-mapped
files.

Resizing I;16B and I;16N images
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
4 changes: 4 additions & 0 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,10 @@ def frombytes(
if self.width == 0 or self.height == 0:
return

# Plugins may call frombytes() during load(), before clearing self.readonly.
if self.im.readonly:
self._copy()

decoder_args: Any = args
if len(decoder_args) == 1 and isinstance(decoder_args[0], tuple):
# may pass tuple instead of argument list
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/ImageCms.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ def point(self, im: Image.Image) -> Image.Image:
def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Image:
if imOut is None:
imOut = Image.new(self.output_mode, im.size)
else:
imOut._ensure_mutable()
self.transform.apply(im.getim(), imOut.getim())
imOut.info["icc_profile"] = self.output_profile.tobytes()
return imOut
Expand Down
Loading