Populate Python palette in fromarray()

This commit is contained in:
Andrew Murray 2022-05-09 18:50:54 +10:00
parent 134023796e
commit ef8fd7f8b9
2 changed files with 16 additions and 0 deletions

View File

@ -80,3 +80,15 @@ def test_fromarray():
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

View File

@ -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