replace custom search with dict in gif optimize

This commit is contained in:
David Schmidt 2013-08-06 11:36:02 +02:00
parent c9df9d0b23
commit 9d43f156a8

View File

@ -357,6 +357,9 @@ def getheader(im, palette=None, info=None):
else:
sourcePalette = im.im.getpalette("RGB")[:768]
else: # L-mode
if palette and isinstance(palette, bytes):
sourcePalette = palette[:768]
else:
sourcePalette = bytearray([i//3 for i in range(768)])
usedPaletteColors = paletteBytes = None
@ -373,26 +376,20 @@ def getheader(im, palette=None, info=None):
# create the new palette if not every color is used
if len(usedPaletteColors) < 256:
paletteBytes = b"";
paletteBytes = b""
newPositions = {}
if im.mode == "P":
i = 0
# pick only the used colors from the palette
for i in usedPaletteColors:
paletteBytes += sourcePalette[i*3:i*3+3]
else:
# add only the used grayscales to the palette
for i in usedPaletteColors:
paletteBytes += o8(i)*3
for oldPosition in usedPaletteColors:
paletteBytes += sourcePalette[oldPosition*3:oldPosition*3+3]
newPositions[oldPosition] = i
i += 1
# TODO improve this, maybe add numpy support
# replace the palette color id of all pixel with the new id
imageBytes = bytearray(im.tobytes())
for i in range(len(imageBytes)):
for newI in range(len(usedPaletteColors)):
if imageBytes[i] == usedPaletteColors[newI]:
imageBytes[i] = newI
break
imageBytes[i] = newPositions[imageBytes[i]]
im.frombytes(bytes(imageBytes))
if not paletteBytes: