mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-10 00:20:57 +03:00
reduce PNG palette size for images <255 colors
limit color and trancparency palette to 2^bits entries, when a PNG is saved with the 'bits' PNG encoder option
This commit is contained in:
parent
6537ba19c3
commit
6a450e303d
|
@ -545,12 +545,16 @@ def _save(im, fp, filename, chunk=putchunk, check=0):
|
||||||
b'\0') # 12: interlace flag
|
b'\0') # 12: interlace flag
|
||||||
|
|
||||||
if im.mode == "P":
|
if im.mode == "P":
|
||||||
chunk(fp, b"PLTE", im.im.getpalette("RGB"))
|
palette_bytes = (2 ** bits) * 3
|
||||||
|
chunk(fp, b"PLTE", im.im.getpalette("RGB")[:palette_bytes])
|
||||||
|
|
||||||
if "transparency" in im.encoderinfo:
|
if "transparency" in im.encoderinfo:
|
||||||
if im.mode == "P":
|
if im.mode == "P":
|
||||||
transparency = max(0, min(255, im.encoderinfo["transparency"]))
|
transparency = max(0, min(255, im.encoderinfo["transparency"]))
|
||||||
chunk(fp, b"tRNS", b'\xFF' * transparency + b'\0')
|
alpha = b'\xFF' * transparency + b'\0'
|
||||||
|
# limit to actual palette size
|
||||||
|
alpha_bytes = 2**bits
|
||||||
|
chunk(fp, b"tRNS", alpha[:alpha_bytes])
|
||||||
elif im.mode == "L":
|
elif im.mode == "L":
|
||||||
transparency = max(0, min(65535, im.encoderinfo["transparency"]))
|
transparency = max(0, min(65535, im.encoderinfo["transparency"]))
|
||||||
chunk(fp, b"tRNS", o16(transparency))
|
chunk(fp, b"tRNS", o16(transparency))
|
||||||
|
@ -562,7 +566,8 @@ def _save(im, fp, filename, chunk=putchunk, check=0):
|
||||||
else:
|
else:
|
||||||
if im.mode == "P" and im.im.getpalettemode() == "RGBA":
|
if im.mode == "P" and im.im.getpalettemode() == "RGBA":
|
||||||
alpha = im.im.getpalette("RGBA", "A")
|
alpha = im.im.getpalette("RGBA", "A")
|
||||||
chunk(fp, "tRNS", alpha)
|
alpha_bytes = 2**bits
|
||||||
|
chunk(fp, b"tRNS", alpha[:alpha_bytes])
|
||||||
|
|
||||||
if 0:
|
if 0:
|
||||||
# FIXME: to be supported some day
|
# FIXME: to be supported some day
|
||||||
|
|
Loading…
Reference in New Issue
Block a user