Merge pull request #2133 from wiredfool/gif-optimize-perf

Speed up GIF save optimization step
This commit is contained in:
wiredfool 2016-10-03 06:03:12 -07:00 committed by GitHub
commit f8e6953e6e
2 changed files with 102 additions and 15 deletions

View File

@ -586,6 +586,10 @@ def _get_header_palette(palette_bytes):
palette_bytes += o8(0) * 3 * actual_target_size_diff palette_bytes += o8(0) * 3 * actual_target_size_diff
return palette_bytes return palette_bytes
# Force optimization so that we can test performance against
# cases where it took lots of memory and time previously.
_FORCE_OPTIMIZE = False
def _get_palette_bytes(im, palette, info): def _get_palette_bytes(im, palette, info):
if im.mode == "P": if im.mode == "P":
if palette and isinstance(palette, bytes): if palette and isinstance(palette, bytes):
@ -603,32 +607,78 @@ def _get_palette_bytes(im, palette, info):
if _get_optimize(im, info): if _get_optimize(im, info):
used_palette_colors = _get_used_palette_colors(im) used_palette_colors = _get_used_palette_colors(im)
# create the new palette if not every color is used # Potentially expensive operation.
if len(used_palette_colors) < 256:
palette_bytes = b"" # The palette saves 3 bytes per color not used, but palette
new_positions = {} # lengths are restricted to 3*(2**N) bytes. Max saving would
# be 768 -> 6 bytes if we went all the way down to 2 colors.
# * If we're over 128 colors, we can't save any space.
# * If there aren't any holes, it's not worth collapsing.
# * If we have a 'large' image, the palette is in the noise.
# create the new palette if not every color is used
if _FORCE_OPTIMIZE or im.mode == 'L' or \
(len(used_palette_colors) <= 128 and
max(used_palette_colors) > len(used_palette_colors) and
im.width * im.height < 512 * 512):
palette_bytes = b""
new_positions = [0]*256
i = 0
# pick only the used colors from the palette # pick only the used colors from the palette
for oldPosition in used_palette_colors: for i, oldPosition in enumerate(used_palette_colors):
palette_bytes += source_palette[oldPosition*3:oldPosition*3+3] palette_bytes += source_palette[oldPosition*3:oldPosition*3+3]
new_positions[oldPosition] = i new_positions[oldPosition] = i
i += 1
# replace the palette color id of all pixel with the new id # replace the palette color id of all pixel with the new id
image_bytes = bytearray(im.tobytes())
for i in range(len(image_bytes)): # Palette images are [0..255], mapped through a 1 or 3
image_bytes[i] = new_positions[image_bytes[i]] # byte/color map. We need to remap the whole image
im.frombytes(bytes(image_bytes)) # from palette 1 to palette 2. New_positions is
# an array of indexes into palette 1. Palette 2 is
# palette 1 with any holes removed.
# We're going to leverage the convert mechanism to use the
# C code to remap the image from palette 1 to palette 2,
# by forcing the source image into 'L' mode and adding a
# mapping 'L' mode palette, then converting back to 'L'
# sans palette thus converting the image bytes, then
# assigning the optimized RGB palette.
# perf reference, 9500x4000 gif, w/~135 colors
# 14 sec prepatch, 1 sec postpatch with optimization forced.
mapping_palette = bytearray(new_positions)
m_im = im.copy()
m_im.mode = 'P'
m_im.palette = ImagePalette.ImagePalette("RGB",
palette=mapping_palette*3,
size=768)
#possibly set palette dirty, then
#m_im.putpalette(mapping_palette, 'L') # converts to 'P'
# or just force it.
# UNDONE -- this is part of the general issue with palettes
m_im.im.putpalette(*m_im.palette.getdata())
m_im = m_im.convert('L')
# Internally, we require 768 bytes for a palette.
new_palette_bytes = (palette_bytes + new_palette_bytes = (palette_bytes +
(768 - len(palette_bytes)) * b'\x00') (768 - len(palette_bytes)) * b'\x00')
im.putpalette(new_palette_bytes) m_im.putpalette(new_palette_bytes)
im.palette = ImagePalette.ImagePalette("RGB", m_im.palette = ImagePalette.ImagePalette("RGB",
palette=palette_bytes, palette=palette_bytes,
size=len(palette_bytes)) size=len(palette_bytes))
# oh gawd, this is modifying the image in place so I can pass by ref.
# REFACTOR SOONEST
im.frombytes(m_im.tobytes())
if not palette_bytes: if not palette_bytes:
palette_bytes = source_palette palette_bytes = source_palette
# returning palette, _not_ padded to 768 bytes like our internal ones.
return palette_bytes, used_palette_colors return palette_bytes, used_palette_colors
def getheader(im, palette=None, info=None): def getheader(im, palette=None, info=None):

View File

@ -3,6 +3,8 @@ from helper import unittest, PillowTestCase, hopper, netpbm_available
from PIL import Image from PIL import Image
from PIL import GifImagePlugin from PIL import GifImagePlugin
from io import BytesIO
codecs = dir(Image.core) codecs = dir(Image.core)
# sample gif stream # sample gif stream
@ -33,8 +35,6 @@ class TestFileGif(PillowTestCase):
lambda: GifImagePlugin.GifImageFile(invalid_file)) lambda: GifImagePlugin.GifImageFile(invalid_file))
def test_optimize(self): def test_optimize(self):
from io import BytesIO
def test_grayscale(optimize): def test_grayscale(optimize):
im = Image.new("L", (1, 1), 0) im = Image.new("L", (1, 1), 0)
filename = BytesIO() filename = BytesIO()
@ -52,6 +52,43 @@ class TestFileGif(PillowTestCase):
self.assertEqual(test_bilevel(0), 800) self.assertEqual(test_bilevel(0), 800)
self.assertEqual(test_bilevel(1), 800) self.assertEqual(test_bilevel(1), 800)
def test_optimize_correctness(self):
# 256 color Palette image, posterize to > 128 and < 128 levels
# Size bigger and smaller than 512x512
# Check the palette for number of colors allocated.
# Check for correctness after conversion back to RGB
def check(colors, size, expected_palette_length):
# make an image with empty colors in the start of the palette range
im = Image.frombytes('P', (colors,colors),
bytes(bytearray(list(range(256-colors,256))*colors)))
im = im.resize((size,size))
outfile = BytesIO()
im.save(outfile, 'GIF')
outfile.seek(0)
reloaded = Image.open(outfile)
# check palette length
palette_length = max(i+1 for i,v in enumerate(reloaded.histogram()) if v)
self.assertEqual(expected_palette_length, palette_length)
self.assert_image_equal(im.convert('RGB'), reloaded.convert('RGB'))
# These do optimize the palette
check(128, 511, 128)
check(64, 511, 64)
check(4, 511, 4)
# These don't optimize the palette
check(128, 513, 256)
check(64, 513, 256)
check(4, 513, 256)
# other limits that don't optimize the palette
check(129, 511, 256)
check(255, 511, 256)
check(256, 511, 256)
def test_optimize_full_l(self): def test_optimize_full_l(self):
from io import BytesIO from io import BytesIO