mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-05-06 00:43:41 +03:00
Set palette to be empty by default
This commit is contained in:
parent
dc89f0eaa9
commit
4d36feefc7
|
@ -606,7 +606,7 @@ class TestImage:
|
||||||
else:
|
else:
|
||||||
assert new_im.palette is None
|
assert new_im.palette is None
|
||||||
|
|
||||||
_make_new(im, im_p, im_p.palette)
|
_make_new(im, im_p, ImagePalette.ImagePalette(list(range(256)) * 3))
|
||||||
_make_new(im_p, im, None)
|
_make_new(im_p, im, None)
|
||||||
_make_new(im, blank_p, ImagePalette.ImagePalette())
|
_make_new(im, blank_p, ImagePalette.ImagePalette())
|
||||||
_make_new(im, blank_pa, ImagePalette.ImagePalette())
|
_make_new(im, blank_pa, ImagePalette.ImagePalette())
|
||||||
|
|
|
@ -11,12 +11,14 @@ def test_sanity():
|
||||||
assert len(palette.colors) == 256
|
assert len(palette.colors) == 256
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
ImagePalette.ImagePalette("RGB", list(range(256)) * 2)
|
ImagePalette.ImagePalette("RGB", list(range(256)) * 3, 10)
|
||||||
|
|
||||||
|
|
||||||
def test_getcolor():
|
def test_getcolor():
|
||||||
|
|
||||||
palette = ImagePalette.ImagePalette()
|
palette = ImagePalette.ImagePalette()
|
||||||
|
assert len(palette.palette) == 0
|
||||||
|
assert len(palette.colors) == 0
|
||||||
|
|
||||||
test_map = {}
|
test_map = {}
|
||||||
for i in range(256):
|
for i in range(256):
|
||||||
|
|
|
@ -1039,6 +1039,10 @@ class Image:
|
||||||
raise ValueError("illegal conversion") from e
|
raise ValueError("illegal conversion") from e
|
||||||
|
|
||||||
new_im = self._new(im)
|
new_im = self._new(im)
|
||||||
|
if mode == "P" and palette != ADAPTIVE:
|
||||||
|
from . import ImagePalette
|
||||||
|
|
||||||
|
new_im.palette = ImagePalette.ImagePalette("RGB", list(range(256)) * 3)
|
||||||
if delete_trns:
|
if delete_trns:
|
||||||
# crash fail if we leave a bytes transparency in an rgb/l mode.
|
# crash fail if we leave a bytes transparency in an rgb/l mode.
|
||||||
del new_im.info["transparency"]
|
del new_im.info["transparency"]
|
||||||
|
@ -1700,7 +1704,7 @@ class Image:
|
||||||
Attaches a palette to this image. The image must be a "P", "PA", "L"
|
Attaches a palette to this image. The image must be a "P", "PA", "L"
|
||||||
or "LA" image.
|
or "LA" image.
|
||||||
|
|
||||||
The palette sequence must contain either 768 integer values, or 1024
|
The palette sequence must contain at most 768 integer values, or 1024
|
||||||
integer values if alpha is included. Each group of values represents
|
integer values if alpha is included. Each group of values represents
|
||||||
the red, green, blue (and alpha if included) values for the
|
the red, green, blue (and alpha if included) values for the
|
||||||
corresponding pixel index. Instead of an integer sequence, you can use
|
corresponding pixel index. Instead of an integer sequence, you can use
|
||||||
|
|
|
@ -39,11 +39,9 @@ class ImagePalette:
|
||||||
def __init__(self, mode="RGB", palette=None, size=0):
|
def __init__(self, mode="RGB", palette=None, size=0):
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.rawmode = None # if set, palette contains raw data
|
self.rawmode = None # if set, palette contains raw data
|
||||||
self.palette = palette or bytearray(range(256)) * len(self.mode)
|
self.palette = palette or bytearray()
|
||||||
self.dirty = None
|
self.dirty = None
|
||||||
if (size == 0 and len(self.mode) * 256 != len(self.palette)) or (
|
if size != 0 and size != len(self.palette):
|
||||||
size != 0 and size != len(self.palette)
|
|
||||||
):
|
|
||||||
raise ValueError("wrong palette size")
|
raise ValueError("wrong palette size")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -113,13 +111,16 @@ class ImagePalette:
|
||||||
# allocate new color slot
|
# allocate new color slot
|
||||||
if isinstance(self.palette, bytes):
|
if isinstance(self.palette, bytes):
|
||||||
self._palette = bytearray(self.palette)
|
self._palette = bytearray(self.palette)
|
||||||
index = len(self.colors)
|
index = len(self.palette) // 3
|
||||||
if index >= 256:
|
if index >= 256:
|
||||||
raise ValueError("cannot allocate more than 256 colors") from e
|
raise ValueError("cannot allocate more than 256 colors") from e
|
||||||
self.colors[color] = index
|
self.colors[color] = index
|
||||||
self._palette[index] = color[0]
|
if index * 3 < len(self.palette):
|
||||||
self._palette[index + 256] = color[1]
|
self._palette[index * 3] = color[0]
|
||||||
self._palette[index + 512] = color[2]
|
self._palette[index * 3 + 1] = color[1]
|
||||||
|
self._palette[index * 3 + 2] = color[2]
|
||||||
|
else:
|
||||||
|
self._palette += bytes(color)
|
||||||
self.dirty = 1
|
self.dirty = 1
|
||||||
return index
|
return index
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user