Skip to content
Closed
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
26 changes: 26 additions & 0 deletions Tests/test_imageops.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,32 @@ def test_invert_keeps_transparent_pixels(
assert result.info["transparency"] == opaque


@pytest.mark.parametrize(
"mode, transparent, opaque, mapped",
(
("L", 31, 224, 16),
("RGB", (250, 10, 10), (10, 250, 10), (240, 0, 0)),
),
)
def test_posterize_updates_transparency(
mode: str,
transparent: int | tuple[int, ...],
opaque: int | tuple[int, ...],
mapped: int | tuple[int, ...],
) -> None:
im = Image.new(mode, (2, 1), transparent)
im.putpixel((1, 0), opaque)
im.info["transparency"] = transparent

result = ImageOps.posterize(im, 4)

assert result.mode == mode
assert result.getpixel((0, 0)) == mapped
assert result.info["transparency"] == mapped
assert result.convert("RGBA").getchannel("A").get_flattened_data() == (0, 255)
assert im.info["transparency"] == transparent


def test_palette_histogram_operation_not_supported() -> None:
im = hopper("P")
with pytest.raises(NotImplementedError, match="mode P support coming soon"):
Expand Down
7 changes: 7 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ Two new image processing operations are available in ImageOps.
Other changes
=============

Update the transparency key when posterizing images
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:py:func:`~PIL.ImageOps.posterize` now maps the transparency key of ``L`` and ``RGB``
images to its posterized value. Colors that map to the same value share the
same transparency key.

PCX and ZIP decoder row sizes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
18 changes: 12 additions & 6 deletions src/PIL/ImageOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,14 @@ def neon(
return Image.blend(image.convert("RGB"), neon, alpha)


def _remap_transparency(image: Image.Image, lut: list[int]) -> None:
transparency = image.info.get("transparency")
if image.mode in {"1", "L"} and isinstance(transparency, int):
image.info["transparency"] = lut[transparency]
elif image.mode == "RGB" and isinstance(transparency, (tuple, list)):
image.info["transparency"] = tuple(lut[channel] for channel in transparency)


def invert(image: Image.Image) -> Image.Image:
"""
Invert (negate) the image.
Expand All @@ -787,11 +795,7 @@ def invert(image: Image.Image) -> Image.Image:
lut = list(range(255, -1, -1))
out = image.point(lut) if image.mode == "1" else _lut(image, lut, palette=True)

transparency = out.info.get("transparency")
if image.mode in {"1", "L"} and isinstance(transparency, int):
out.info["transparency"] = lut[transparency]
elif image.mode == "RGB" and isinstance(transparency, (tuple, list)):
out.info["transparency"] = tuple(lut[channel] for channel in transparency)
_remap_transparency(out, lut)
return out


Expand All @@ -818,7 +822,9 @@ def posterize(image: Image.Image, bits: int) -> Image.Image:
raise ValueError(msg)
mask = ~(2 ** (8 - bits) - 1)
lut = [i & mask for i in range(256)]
return _lut(image, lut, palette=True)
out = _lut(image, lut, palette=True)
_remap_transparency(out, lut)
return out


def solarize(image: Image.Image, threshold: int = 128) -> Image.Image:
Expand Down
Loading