Skip to content

Commit

Permalink
Merge pull request #6283 from radarhere/palette
Browse files Browse the repository at this point in the history
Populate Python palette in fromarray()
  • Loading branch information
hugovk authored May 9, 2022
2 parents 1340237 + ef8fd7f commit 5e0e1e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Tests/test_image_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ def test(mode):
with pytest.raises(TypeError):
wrapped = Wrapper(test("L"), {"shape": (100, 128)})
Image.fromarray(wrapped)


def test_fromarray_palette():
# Arrange
i = im.convert("L")
a = numpy.array(i)

# Act
out = Image.fromarray(a, "P")

# Assert that the Python and C palettes match
assert len(out.palette.colors) == len(out.im.getpalette()) / 3
4 changes: 4 additions & 0 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2838,6 +2838,10 @@ def frombuffer(mode, size, data, decoder_name="raw", *args):
if args[0] in _MAPMODES:
im = new(mode, (1, 1))
im = im._new(core.map_buffer(data, size, decoder_name, 0, args))
if mode == "P":
from . import ImagePalette

im.palette = ImagePalette.ImagePalette("RGB", im.im.getpalette("RGB"))
im.readonly = 1
return im

Expand Down

0 comments on commit 5e0e1e4

Please sign in to comment.