Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow RGB and RGBA values for P image putpixel #3519

Merged
merged 2 commits into from
Jan 1, 2019
Merged
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
13 changes: 13 additions & 0 deletions Tests/test_image_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ def test_signedness(self):
self.check(mode, 2**15+1)
self.check(mode, 2**16-1)

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")
class TestCffiPutPixel(TestImagePutPixel):
Expand Down Expand Up @@ -294,6 +300,13 @@ def test_reference_counting(self):
# pixels can contain garbage if image is released
self.assertEqual(px[i, 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):
@unittest.skipIf(not sys.platform.startswith('win32') or
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/PixelAccess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +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
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)
Expand Down
8 changes: 7 additions & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,8 @@ def putpixel(self, xy, value):
"""
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.
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`
Expand All @@ -1674,6 +1675,11 @@ def putpixel(self, xy, value):

if self.pyaccess:
return self.pyaccess.putpixel(xy, value)

if self.mode == "P" and \
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)

def remap_palette(self, dest_map, source_palette=None):
Expand Down
8 changes: 8 additions & 0 deletions src/PIL/PyAccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def __init__(self, img, readonly=False):

# Keep pointer to im object to prevent dereferencing.
self._im = img.im
if self._im.mode == "P":
self._palette = img.palette

# Debugging is polluting test traces, only useful here
# when hacking on PyAccess
Expand Down Expand Up @@ -80,6 +82,12 @@ def __setitem__(self, xy, color):
if y < 0:
y = self.ysize + y
(x, y) = self.check_xy((x, y))

if self._im.mode == "P" and \
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)

def __getitem__(self, xy):
Expand Down