Allow RGBA value for P image putpixel

This commit is contained in:
Andrew Murray 2018-12-31 13:37:04 +11:00
parent 3f6282e259
commit 671f7a392d
4 changed files with 19 additions and 17 deletions

View File

@ -175,9 +175,10 @@ class TestImageGetPixel(AccessTest):
self.check(mode, 2**15+1) self.check(mode, 2**15+1)
self.check(mode, 2**16-1) self.check(mode, 2**16-1)
def test_p_putpixel_rgb(self): def test_p_putpixel_rgb_rgba(self):
for color in [(255, 0, 0), (255, 0, 0, 255)]:
im = Image.new("P", (1, 1), 0) im = Image.new("P", (1, 1), 0)
im.putpixel((0, 0), (255, 0, 0)) im.putpixel((0, 0), color)
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0)) self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0))
@ -299,10 +300,11 @@ class TestCffi(AccessTest):
# pixels can contain garbage if image is released # pixels can contain garbage if image is released
self.assertEqual(px[i, 0], 0) self.assertEqual(px[i, 0], 0)
def test_p_putpixel_rgb(self): def test_p_putpixel_rgb_rgba(self):
for color in [(255, 0, 0), (255, 0, 0, 255)]:
im = Image.new("P", (1, 1), 0) im = Image.new("P", (1, 1), 0)
access = PyAccess.new(im, False) access = PyAccess.new(im, False)
access.putpixel((0, 0), (255, 0, 0)) access.putpixel((0, 0), color)
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0)) self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0))

View File

@ -65,8 +65,8 @@ Access using negative indexes is also possible.
Modifies the pixel at x,y. The color is given as a single Modifies the pixel at x,y. The color is given as a single
numerical value for single band images, and a tuple for numerical value for single band images, and a tuple for
multi-band images. In addition to this, RGB tuples are accepted multi-band images. In addition to this, RGB and RGBA tuples
for P images. are accepted for P images.
:param xy: The pixel coordinate, given as (x, y). :param xy: The pixel coordinate, given as (x, y).
:param color: The pixel value according to its mode. e.g. tuple (r, g, b) for RGB mode) :param color: The pixel value according to its mode. e.g. tuple (r, g, b) for RGB mode)

View File

@ -1651,8 +1651,8 @@ class Image(object):
""" """
Modifies the pixel at the given position. The color is given as Modifies the pixel at the given position. The color is given as
a single numerical value for single-band images, and a tuple for a single numerical value for single-band images, and a tuple for
multi-band images. In addition to this, RGB tuples are accepted multi-band images. In addition to this, RGB and RGBA tuples are
for P images. accepted for P images.
Note that this method is relatively slow. For more extensive changes, Note that this method is relatively slow. For more extensive changes,
use :py:meth:`~PIL.Image.Image.paste` or the :py:mod:`~PIL.ImageDraw` use :py:meth:`~PIL.Image.Image.paste` or the :py:mod:`~PIL.ImageDraw`
@ -1677,8 +1677,8 @@ class Image(object):
return self.pyaccess.putpixel(xy, value) return self.pyaccess.putpixel(xy, value)
if self.mode == "P" and \ if self.mode == "P" and \
isinstance(value, (list, tuple)) and len(value) == 3: isinstance(value, (list, tuple)) and len(value) in [3, 4]:
# RGB value for a P image # RGB or RGBA value for a P image
value = self.palette.getcolor(value) value = self.palette.getcolor(value)
return self.im.putpixel(xy, value) return self.im.putpixel(xy, value)

View File

@ -84,8 +84,8 @@ class PyAccess(object):
(x, y) = self.check_xy((x, y)) (x, y) = self.check_xy((x, y))
if self._im.mode == "P" and \ if self._im.mode == "P" and \
isinstance(color, (list, tuple)) and len(color) == 3: isinstance(color, (list, tuple)) and len(color) in [3, 4]:
# RGB value for a P image # RGB or RGBA value for a P image
color = self._palette.getcolor(color) color = self._palette.getcolor(color)
return self.set_pixel(x, y, color) return self.set_pixel(x, y, color)