Skip to content

Commit 762c29c

Browse files
authored
Merge pull request #6504 from radarhere/pyaccess_pa
Allow RGB and RGBA values for PA image putpixel
2 parents 06660a5 + 2eca298 commit 762c29c

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

Tests/test_image_access.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,14 @@ def test_signedness(self, mode):
215215
self.check(mode, 2**15 + 1)
216216
self.check(mode, 2**16 - 1)
217217

218+
@pytest.mark.parametrize("mode", ("P", "PA"))
218219
@pytest.mark.parametrize("color", ((255, 0, 0), (255, 0, 0, 255)))
219-
def test_p_putpixel_rgb_rgba(self, color):
220-
im = Image.new("P", (1, 1), 0)
220+
def test_p_putpixel_rgb_rgba(self, mode, color):
221+
im = Image.new(mode, (1, 1))
221222
im.putpixel((0, 0), color)
222-
assert im.convert("RGB").getpixel((0, 0)) == (255, 0, 0)
223+
224+
alpha = color[3] if len(color) == 4 and mode == "PA" else 255
225+
assert im.convert("RGBA").getpixel((0, 0)) == (255, 0, 0, alpha)
223226

224227

225228
@pytest.mark.skipif(cffi is None, reason="No CFFI")
@@ -340,12 +343,15 @@ def test_reference_counting(self):
340343
# pixels can contain garbage if image is released
341344
assert px[i, 0] == 0
342345

343-
def test_p_putpixel_rgb_rgba(self):
344-
for color in [(255, 0, 0), (255, 0, 0, 255)]:
345-
im = Image.new("P", (1, 1), 0)
346+
@pytest.mark.parametrize("mode", ("P", "PA"))
347+
def test_p_putpixel_rgb_rgba(self, mode):
348+
for color in [(255, 0, 0), (255, 0, 0, 127)]:
349+
im = Image.new(mode, (1, 1))
346350
access = PyAccess.new(im, False)
347351
access.putpixel((0, 0), color)
348-
assert im.convert("RGB").getpixel((0, 0)) == (255, 0, 0)
352+
353+
alpha = color[3] if len(color) == 4 and mode == "PA" else 255
354+
assert im.convert("RGBA").getpixel((0, 0)) == (255, 0, 0, alpha)
349355

350356

351357
class TestImagePutPixelError(AccessTest):

docs/reference/PixelAccess.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Access using negative indexes is also possible.
7373
Modifies the pixel at x,y. The color is given as a single
7474
numerical value for single band images, and a tuple for
7575
multi-band images. In addition to this, RGB and RGBA tuples
76-
are accepted for P images.
76+
are accepted for P and PA images.
7777

7878
:param xy: The pixel coordinate, given as (x, y).
7979
:param color: The pixel value according to its mode. e.g. tuple (r, g, b) for RGB mode)

src/PIL/Image.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@ def putpixel(self, xy, value):
18391839
Modifies the pixel at the given position. The color is given as
18401840
a single numerical value for single-band images, and a tuple for
18411841
multi-band images. In addition to this, RGB and RGBA tuples are
1842-
accepted for P images.
1842+
accepted for P and PA images.
18431843
18441844
Note that this method is relatively slow. For more extensive changes,
18451845
use :py:meth:`~PIL.Image.Image.paste` or the :py:mod:`~PIL.ImageDraw`
@@ -1864,12 +1864,17 @@ def putpixel(self, xy, value):
18641864
return self.pyaccess.putpixel(xy, value)
18651865

18661866
if (
1867-
self.mode == "P"
1867+
self.mode in ("P", "PA")
18681868
and isinstance(value, (list, tuple))
18691869
and len(value) in [3, 4]
18701870
):
1871-
# RGB or RGBA value for a P image
1871+
# RGB or RGBA value for a P or PA image
1872+
if self.mode == "PA":
1873+
alpha = value[3] if len(value) == 4 else 255
1874+
value = value[:3]
18721875
value = self.palette.getcolor(value, self)
1876+
if self.mode == "PA":
1877+
value = (value, alpha)
18731878
return self.im.putpixel(xy, value)
18741879

18751880
def remap_palette(self, dest_map, source_palette=None):

src/PIL/PyAccess.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, img, readonly=False):
5858

5959
# Keep pointer to im object to prevent dereferencing.
6060
self._im = img.im
61-
if self._im.mode == "P":
61+
if self._im.mode in ("P", "PA"):
6262
self._palette = img.palette
6363

6464
# Debugging is polluting test traces, only useful here
@@ -89,12 +89,17 @@ def __setitem__(self, xy, color):
8989
(x, y) = self.check_xy((x, y))
9090

9191
if (
92-
self._im.mode == "P"
92+
self._im.mode in ("P", "PA")
9393
and isinstance(color, (list, tuple))
9494
and len(color) in [3, 4]
9595
):
96-
# RGB or RGBA value for a P image
96+
# RGB or RGBA value for a P or PA image
97+
if self._im.mode == "PA":
98+
alpha = color[3] if len(color) == 4 else 255
99+
color = color[:3]
97100
color = self._palette.getcolor(color, self._img)
101+
if self._im.mode == "PA":
102+
color = (color, alpha)
98103

99104
return self.set_pixel(x, y, color)
100105

0 commit comments

Comments
 (0)