mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 18:06:18 +03:00
replace custom search with dict in gif optimize
This commit is contained in:
parent
c9df9d0b23
commit
9d43f156a8
|
@ -357,6 +357,9 @@ def getheader(im, palette=None, info=None):
|
||||||
else:
|
else:
|
||||||
sourcePalette = im.im.getpalette("RGB")[:768]
|
sourcePalette = im.im.getpalette("RGB")[:768]
|
||||||
else: # L-mode
|
else: # L-mode
|
||||||
|
if palette and isinstance(palette, bytes):
|
||||||
|
sourcePalette = palette[:768]
|
||||||
|
else:
|
||||||
sourcePalette = bytearray([i//3 for i in range(768)])
|
sourcePalette = bytearray([i//3 for i in range(768)])
|
||||||
|
|
||||||
usedPaletteColors = paletteBytes = None
|
usedPaletteColors = paletteBytes = None
|
||||||
|
@ -373,26 +376,20 @@ def getheader(im, palette=None, info=None):
|
||||||
|
|
||||||
# create the new palette if not every color is used
|
# create the new palette if not every color is used
|
||||||
if len(usedPaletteColors) < 256:
|
if len(usedPaletteColors) < 256:
|
||||||
paletteBytes = b"";
|
paletteBytes = b""
|
||||||
|
newPositions = {}
|
||||||
|
|
||||||
if im.mode == "P":
|
i = 0
|
||||||
# pick only the used colors from the palette
|
# pick only the used colors from the palette
|
||||||
for i in usedPaletteColors:
|
for oldPosition in usedPaletteColors:
|
||||||
paletteBytes += sourcePalette[i*3:i*3+3]
|
paletteBytes += sourcePalette[oldPosition*3:oldPosition*3+3]
|
||||||
else:
|
newPositions[oldPosition] = i
|
||||||
# add only the used grayscales to the palette
|
i += 1
|
||||||
for i in usedPaletteColors:
|
|
||||||
paletteBytes += o8(i)*3
|
|
||||||
|
|
||||||
# TODO improve this, maybe add numpy support
|
|
||||||
# replace the palette color id of all pixel with the new id
|
# replace the palette color id of all pixel with the new id
|
||||||
imageBytes = bytearray(im.tobytes())
|
imageBytes = bytearray(im.tobytes())
|
||||||
for i in range(len(imageBytes)):
|
for i in range(len(imageBytes)):
|
||||||
for newI in range(len(usedPaletteColors)):
|
imageBytes[i] = newPositions[imageBytes[i]]
|
||||||
if imageBytes[i] == usedPaletteColors[newI]:
|
|
||||||
imageBytes[i] = newI
|
|
||||||
break
|
|
||||||
|
|
||||||
im.frombytes(bytes(imageBytes))
|
im.frombytes(bytes(imageBytes))
|
||||||
|
|
||||||
if not paletteBytes:
|
if not paletteBytes:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user