mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-04 20:03:20 +03:00
If all 256 colors are in use, then there is no need for an additional color for background or transparency
This commit is contained in:
parent
fa559277fb
commit
8210645e4b
|
@ -31,8 +31,11 @@ def test_getcolor():
|
||||||
|
|
||||||
assert palette.getcolor((0, 0, 0)) == palette.getcolor((0, 0, 0, 255))
|
assert palette.getcolor((0, 0, 0)) == palette.getcolor((0, 0, 0, 255))
|
||||||
|
|
||||||
|
# An error is raised when the palette is full
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
palette.getcolor((1, 2, 3))
|
palette.getcolor((1, 2, 3))
|
||||||
|
# But not if the image is not using one of the palette entries
|
||||||
|
palette.getcolor((1, 2, 3), image=Image.new("P", (1, 1)))
|
||||||
|
|
||||||
# Test unknown color specifier
|
# Test unknown color specifier
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
|
|
|
@ -760,7 +760,15 @@ def _get_background(im, infoBackground):
|
||||||
# WebPImagePlugin stores an RGBA value in info["background"]
|
# WebPImagePlugin stores an RGBA value in info["background"]
|
||||||
# So it must be converted to the same format as GifImagePlugin's
|
# So it must be converted to the same format as GifImagePlugin's
|
||||||
# info["background"] - a global color table index
|
# info["background"] - a global color table index
|
||||||
background = im.palette.getcolor(background, im)
|
try:
|
||||||
|
background = im.palette.getcolor(background, im)
|
||||||
|
except ValueError as e:
|
||||||
|
if str(e) == "cannot allocate more than 256 colors":
|
||||||
|
# If all 256 colors are in use,
|
||||||
|
# then there is no need for the background color
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
raise
|
||||||
return background
|
return background
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -979,19 +979,27 @@ class Image:
|
||||||
if isinstance(t, tuple):
|
if isinstance(t, tuple):
|
||||||
try:
|
try:
|
||||||
t = trns_im.palette.getcolor(t, self)
|
t = trns_im.palette.getcolor(t, self)
|
||||||
except Exception as e:
|
except ValueError as e:
|
||||||
raise ValueError(
|
if str(e) == "cannot allocate more than 256 colors":
|
||||||
"Couldn't allocate a palette color for transparency"
|
# If all 256 colors are in use,
|
||||||
) from e
|
# then there is no need for transparency
|
||||||
trns_im.putpixel((0, 0), t)
|
t = None
|
||||||
|
else:
|
||||||
if mode in ("L", "RGB"):
|
raise ValueError(
|
||||||
trns_im = trns_im.convert(mode)
|
"Couldn't allocate a palette color for transparency"
|
||||||
|
) from e
|
||||||
|
if t is None:
|
||||||
|
trns = None
|
||||||
else:
|
else:
|
||||||
# can't just retrieve the palette number, got to do it
|
trns_im.putpixel((0, 0), t)
|
||||||
# after quantization.
|
|
||||||
trns_im = trns_im.convert("RGB")
|
if mode in ("L", "RGB"):
|
||||||
trns = trns_im.getpixel((0, 0))
|
trns_im = trns_im.convert(mode)
|
||||||
|
else:
|
||||||
|
# can't just retrieve the palette number, got to do it
|
||||||
|
# after quantization.
|
||||||
|
trns_im = trns_im.convert("RGB")
|
||||||
|
trns = trns_im.getpixel((0, 0))
|
||||||
|
|
||||||
elif self.mode == "P" and mode == "RGBA":
|
elif self.mode == "P" and mode == "RGBA":
|
||||||
t = self.info["transparency"]
|
t = self.info["transparency"]
|
||||||
|
@ -1050,9 +1058,14 @@ class Image:
|
||||||
if new_im.mode == "P":
|
if new_im.mode == "P":
|
||||||
try:
|
try:
|
||||||
new_im.info["transparency"] = new_im.palette.getcolor(trns, new_im)
|
new_im.info["transparency"] = new_im.palette.getcolor(trns, new_im)
|
||||||
except Exception:
|
except ValueError as e:
|
||||||
del new_im.info["transparency"]
|
del new_im.info["transparency"]
|
||||||
warnings.warn("Couldn't allocate palette entry for transparency")
|
if str(e) != "cannot allocate more than 256 colors":
|
||||||
|
# If all 256 colors are in use,
|
||||||
|
# then there is no need for transparency
|
||||||
|
warnings.warn(
|
||||||
|
"Couldn't allocate palette entry for transparency"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
new_im.info["transparency"] = trns
|
new_im.info["transparency"] = trns
|
||||||
return new_im
|
return new_im
|
||||||
|
|
Loading…
Reference in New Issue
Block a user