mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-13 05:06:49 +03:00
a break
This commit is contained in:
parent
dad5e86226
commit
12cea19280
|
@ -252,7 +252,7 @@ def _save(im, fp, filename):
|
||||||
palette = im.encoderinfo["palette"]
|
palette = im.encoderinfo["palette"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
palette = None
|
palette = None
|
||||||
|
|
||||||
for s in getheader(imOut, palette, im.encoderinfo):
|
for s in getheader(imOut, palette, im.encoderinfo):
|
||||||
fp.write(s)
|
fp.write(s)
|
||||||
|
|
||||||
|
@ -302,7 +302,7 @@ def _save(im, fp, filename):
|
||||||
try:
|
try:
|
||||||
fp.flush()
|
fp.flush()
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
|
|
||||||
def _save_netpbm(im, fp, filename):
|
def _save_netpbm(im, fp, filename):
|
||||||
|
|
||||||
|
@ -335,13 +335,13 @@ def getheader(im, palette=None, info=None):
|
||||||
o16(im.size[0]) + # size
|
o16(im.size[0]) + # size
|
||||||
o16(im.size[1])
|
o16(im.size[1])
|
||||||
]
|
]
|
||||||
|
|
||||||
# if the user adds a palette, use it
|
# if the user adds a palette, use it
|
||||||
if palette is not None and isinstance(palette, bytes):
|
if palette is not None and isinstance(palette, bytes):
|
||||||
paletteBytes = palette
|
paletteBytes = palette
|
||||||
else:
|
else:
|
||||||
usedPaletteColors = []
|
usedPaletteColors = []
|
||||||
|
|
||||||
if optimize:
|
if optimize:
|
||||||
# minimize color palette if wanted
|
# minimize color palette if wanted
|
||||||
i = 0
|
i = 0
|
||||||
|
@ -349,9 +349,9 @@ def getheader(im, palette=None, info=None):
|
||||||
if count:
|
if count:
|
||||||
usedPaletteColors.append(i)
|
usedPaletteColors.append(i)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
countUsedPaletteColors = len(usedPaletteColors)
|
countUsedPaletteColors = len(usedPaletteColors)
|
||||||
|
|
||||||
# create the global palette
|
# create the global palette
|
||||||
if im.mode == "P":
|
if im.mode == "P":
|
||||||
# colour palette
|
# colour palette
|
||||||
|
@ -371,18 +371,19 @@ def getheader(im, palette=None, info=None):
|
||||||
paletteBytes += o8(i)*3
|
paletteBytes += o8(i)*3
|
||||||
else :
|
else :
|
||||||
paletteBytes = bytes([i//3 for i in range(768)])
|
paletteBytes = bytes([i//3 for i in range(768)])
|
||||||
|
|
||||||
# TODO improve this, maybe add numpy support
|
# 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
|
||||||
if countUsedPaletteColors > 0 and countUsedPaletteColors < 256:
|
if countUsedPaletteColors > 0 and countUsedPaletteColors < 256:
|
||||||
imageBytes = bytearray(im.tobytes())
|
imageBytes = bytearray(im.tobytes())
|
||||||
for i in range(len(imageBytes)):
|
for i in range(len(imageBytes)):
|
||||||
for newI in range(countUsedPaletteColors):
|
for newI in range(countUsedPaletteColors):
|
||||||
if imageBytes[i] == usedPaletteColors[newI]:
|
if imageBytes[i] == usedPaletteColors[newI]:
|
||||||
imageBytes[i] = newI
|
imageBytes[i] = newI
|
||||||
|
break
|
||||||
|
|
||||||
im.frombytes(bytes(imageBytes))
|
im.frombytes(bytes(imageBytes))
|
||||||
|
|
||||||
# calculate the palette size for the header
|
# calculate the palette size for the header
|
||||||
import math
|
import math
|
||||||
colorTableSize = math.ceil(math.log(len(paletteBytes)//3, 2))-1
|
colorTableSize = math.ceil(math.log(len(paletteBytes)//3, 2))-1
|
||||||
|
@ -390,17 +391,17 @@ def getheader(im, palette=None, info=None):
|
||||||
s.append(o8(colorTableSize + 128)) # size of global color table + global color table flag
|
s.append(o8(colorTableSize + 128)) # size of global color table + global color table flag
|
||||||
s.append(o8(0) + o8(0)) # background + reserved/aspect
|
s.append(o8(0) + o8(0)) # background + reserved/aspect
|
||||||
# end of screen descriptor header
|
# end of screen descriptor header
|
||||||
|
|
||||||
# add the missing amount of bytes
|
# add the missing amount of bytes
|
||||||
# the palette has to be 2<<n in size
|
# the palette has to be 2<<n in size
|
||||||
actualTargetSizeDiff = (2<<colorTableSize) - len(paletteBytes)//3
|
actualTargetSizeDiff = (2<<colorTableSize) - len(paletteBytes)//3
|
||||||
if actualTargetSizeDiff > 0:
|
if actualTargetSizeDiff > 0:
|
||||||
paletteBytes += o8(0) * 3 * actualTargetSizeDiff
|
paletteBytes += o8(0) * 3 * actualTargetSizeDiff
|
||||||
|
|
||||||
# global color palette
|
# global color palette
|
||||||
s.append(paletteBytes)
|
s.append(paletteBytes)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def getdata(im, offset = (0, 0), **params):
|
def getdata(im, offset = (0, 0), **params):
|
||||||
"""Return a list of strings representing this image.
|
"""Return a list of strings representing this image.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user