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,10 +175,11 @@ class TestImageGetPixel(AccessTest):
self.check(mode, 2**15+1)
self.check(mode, 2**16-1)
def test_p_putpixel_rgb(self):
im = Image.new("P", (1, 1), 0)
im.putpixel((0, 0), (255, 0, 0))
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0))
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.putpixel((0, 0), color)
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0))
@unittest.skipIf(cffi is None, "No cffi")
@ -299,11 +300,12 @@ class TestCffi(AccessTest):
# pixels can contain garbage if image is released
self.assertEqual(px[i, 0], 0)
def test_p_putpixel_rgb(self):
im = Image.new("P", (1, 1), 0)
access = PyAccess.new(im, False)
access.putpixel((0, 0), (255, 0, 0))
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0))
def test_p_putpixel_rgb_rgba(self):
for color in [(255, 0, 0), (255, 0, 0, 255)]:
im = Image.new("P", (1, 1), 0)
access = PyAccess.new(im, False)
access.putpixel((0, 0), color)
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0))
class TestEmbeddable(unittest.TestCase):

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
numerical value for single band images, and a tuple for
multi-band images. In addition to this, RGB tuples are accepted
for P images.
multi-band images. In addition to this, RGB and RGBA tuples
are accepted for P images.
: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)

View File

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

View File

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