Skip to content

Commit a37593f

Browse files
committed
Allow RGB and RGBA values for PA image putpixel
1 parent 5a087cc commit a37593f

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

Tests/test_image_access.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,14 @@ def test_signedness(self):
212212
self.check(mode, 2**15 + 1)
213213
self.check(mode, 2**16 - 1)
214214

215-
def test_p_putpixel_rgb_rgba(self):
216-
for color in [(255, 0, 0), (255, 0, 0, 255)]:
217-
im = Image.new("P", (1, 1), 0)
215+
@pytest.mark.parametrize("mode", ("P", "PA"))
216+
def test_p_putpixel_rgb_rgba(self, mode):
217+
for color in [(255, 0, 0), (255, 0, 0, 127)]:
218+
im = Image.new(mode, (1, 1))
218219
im.putpixel((0, 0), color)
219-
assert im.convert("RGB").getpixel((0, 0)) == (255, 0, 0)
220+
221+
alpha = color[3] if len(color) == 4 and mode == "PA" else 255
222+
assert im.convert("RGBA").getpixel((0, 0)) == (255, 0, 0, alpha)
220223

221224

222225
@pytest.mark.skipif(cffi is None, reason="No CFFI")
@@ -337,12 +340,15 @@ def test_reference_counting(self):
337340
# pixels can contain garbage if image is released
338341
assert px[i, 0] == 0
339342

340-
def test_p_putpixel_rgb_rgba(self):
341-
for color in [(255, 0, 0), (255, 0, 0, 255)]:
342-
im = Image.new("P", (1, 1), 0)
343+
@pytest.mark.parametrize("mode", ("P", "PA"))
344+
def test_p_putpixel_rgb_rgba(self, mode):
345+
for color in [(255, 0, 0), (255, 0, 0, 127)]:
346+
im = Image.new(mode, (1, 1))
343347
access = PyAccess.new(im, False)
344348
access.putpixel((0, 0), color)
345-
assert im.convert("RGB").getpixel((0, 0)) == (255, 0, 0)
349+
350+
alpha = color[3] if len(color) == 4 and mode == "PA" else 255
351+
assert im.convert("RGBA").getpixel((0, 0)) == (255, 0, 0, alpha)
346352

347353

348354
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)