mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-11 04:07:21 +03:00
adjust the transparency index after successful optimize
skip transparency block if transparent color is not used after optimize
This commit is contained in:
parent
71b30352d9
commit
b66d888b0e
|
@ -253,7 +253,8 @@ def _save(im, fp, filename):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
palette = None
|
palette = None
|
||||||
|
|
||||||
for s in getheader(imOut, palette, im.encoderinfo):
|
header, usedPaletteColors = getheader(imOut, palette, im.encoderinfo)
|
||||||
|
for s in header:
|
||||||
fp.write(s)
|
fp.write(s)
|
||||||
|
|
||||||
flags = 0
|
flags = 0
|
||||||
|
@ -275,13 +276,27 @@ def _save(im, fp, filename):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
transparency = int(transparency)
|
||||||
|
# optimize the block away if transparent color is not used
|
||||||
|
transparentColorExists = True
|
||||||
|
# adjust the transparency index after optimize
|
||||||
|
if usedPaletteColors is not None and len(usedPaletteColors) < 256:
|
||||||
|
for i in range(len(usedPaletteColors)):
|
||||||
|
if usedPaletteColors[i] == transparency:
|
||||||
|
transparency = i
|
||||||
|
transparentColorExists = True
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
transparentColorExists = False
|
||||||
|
|
||||||
# transparency extension block
|
# transparency extension block
|
||||||
|
if transparentColorExists:
|
||||||
fp.write(b"!" +
|
fp.write(b"!" +
|
||||||
o8(249) + # extension intro
|
o8(249) + # extension intro
|
||||||
o8(4) + # length
|
o8(4) + # length
|
||||||
o8(1) + # transparency info present
|
o8(1) + # transparency info present
|
||||||
o16(0) + # duration
|
o16(0) + # duration
|
||||||
o8(int(transparency)) # transparency index
|
o8(transparency) # transparency index
|
||||||
+ o8(0))
|
+ o8(0))
|
||||||
|
|
||||||
# local image header
|
# local image header
|
||||||
|
@ -330,13 +345,15 @@ def getheader(im, palette=None, info=None):
|
||||||
optimize = info and info.get("optimize", 0)
|
optimize = info and info.get("optimize", 0)
|
||||||
|
|
||||||
# start of header
|
# start of header
|
||||||
s = [
|
header = [
|
||||||
b"GIF87a" + # magic
|
b"GIF87a" + # magic
|
||||||
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
|
||||||
|
usedPaletteColors = None
|
||||||
|
|
||||||
if palette is not None and isinstance(palette, bytes):
|
if palette is not None and isinstance(palette, bytes):
|
||||||
paletteBytes = palette[:768]
|
paletteBytes = palette[:768]
|
||||||
else:
|
else:
|
||||||
|
@ -388,8 +405,8 @@ def getheader(im, palette=None, info=None):
|
||||||
import math
|
import math
|
||||||
colorTableSize = int(math.ceil(math.log(len(paletteBytes)//3, 2)))-1
|
colorTableSize = int(math.ceil(math.log(len(paletteBytes)//3, 2)))-1
|
||||||
if colorTableSize < 0: colorTableSize = 0
|
if colorTableSize < 0: colorTableSize = 0
|
||||||
s.append(o8(colorTableSize + 128)) # size of global color table + global color table flag
|
header.append(o8(colorTableSize + 128)) # size of global color table + global color table flag
|
||||||
s.append(o8(0) + o8(0)) # background + reserved/aspect
|
header.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
|
||||||
|
@ -399,8 +416,8 @@ def getheader(im, palette=None, info=None):
|
||||||
paletteBytes += o8(0) * 3 * actualTargetSizeDiff
|
paletteBytes += o8(0) * 3 * actualTargetSizeDiff
|
||||||
|
|
||||||
# global color palette
|
# global color palette
|
||||||
s.append(paletteBytes)
|
header.append(paletteBytes)
|
||||||
return s
|
return header, usedPaletteColors
|
||||||
|
|
||||||
|
|
||||||
def getdata(im, offset = (0, 0), **params):
|
def getdata(im, offset = (0, 0), **params):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user