Merge pull request #6283 from radarhere/palette

Populate Python palette in fromarray()
This commit is contained in:
Hugo van Kemenade 2022-05-09 14:32:34 +03:00 committed by GitHub
commit 5e0e1e4114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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