Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
data = unittest.__doc__
def test_reversible(self):
data = self.data
self.assertEquals(self.decompress(self.compress(data)), data)
def test_iterations_help(self):
data = self.data
self.assertTrue(len(self.compress(data, numiterations=1)) > len(self.compress(data, numiterations=1000)))
class ZlibTest(unittest.TestCase, Tests):
compress = staticmethod(zopfli.zlib.compress)
decompress = staticmethod(zlib.decompress)
class GzipTest(unittest.TestCase, Tests):
compress = staticmethod(zopfli.gzip.compress)
def decompress(self, s):
return gzip.GzipFile(fileobj=StringIO.StringIO(s)).read()
if __name__ == "__main__":
unittest.main()
import zopfli.gzip
import zopfli.zlib
import StringIO
class Tests(object):
data = unittest.__doc__
def test_reversible(self):
data = self.data
self.assertEquals(self.decompress(self.compress(data)), data)
def test_iterations_help(self):
data = self.data
self.assertTrue(len(self.compress(data, numiterations=1)) > len(self.compress(data, numiterations=1000)))
class ZlibTest(unittest.TestCase, Tests):
compress = staticmethod(zopfli.zlib.compress)
decompress = staticmethod(zlib.decompress)
class GzipTest(unittest.TestCase, Tests):
compress = staticmethod(zopfli.gzip.compress)
def decompress(self, s):
return gzip.GzipFile(fileobj=StringIO.StringIO(s)).read()
if __name__ == "__main__":
unittest.main()
:rtype: Asset
"""
mime_type = MimeType(mime_type)
pil_format = PillowProcessor.__mime_type_to_pillow_type[mime_type]
pil_options = dict(PillowProcessor.__format_defaults.get(mime_type, {}))
format_config = dict(self.config.get(mime_type.type, {}))
format_config.update(self.config.get(str(mime_type), {}))
image_buffer = io.BytesIO()
if mime_type == MimeType('image/png') and image.mode != 'P':
use_zopfli = format_config.get('zopfli', False)
if use_zopfli:
import zopfli
zopfli_png = zopfli.ZopfliPNG()
# Convert 16-bit per channel images to 8-bit per channel
zopfli_png.lossy_8bit = False
# Allow altering hidden colors of fully transparent pixels
zopfli_png.lossy_transparent = True
# Use all available optimization strategies
zopfli_png.filter_strategies = format_config.get('zopfli_strategies', '0me')
pil_options.pop('optimize', False)
essence = io.BytesIO()
image.save(essence, 'PNG', optimize=False, **pil_options)
essence.seek(0)
optimized_data = zopfli_png.optimize(essence.read())
image_buffer.write(optimized_data)
else:
image.save(image_buffer, pil_format, **pil_options)
elif mime_type == MimeType('image/jpeg'):
output_format = "jpeg"
# convert / optimize
output_image_bytes = None
if output_format == "jpeg":
output_image_bytes = pyguetzli.process_pil_image(
image, int(options["jpeg_quality"] * 100))
else:
pass
image_io = io.BytesIO()
image.save(image_io, format="PNG", optimize=False)
image_io.seek(0)
image_bytes = image_io.read()
# Optimize using zopflipng
zopflipng = zopfli.ZopfliPNG()
zopflipng.lossy_8bit = True
zopflipng.lossy_transparent = True
zopflipng.filter_strategies = "01234mepb"
zopflipng.iterations = 20
zopflipng.iterations_large = 7
output_image_bytes = zopflipng.optimize(image_bytes)
# write to output_file
output_file.seek(0)
output_file.truncate()
output_file.write(output_image_bytes)
def do_compress(self, filename, compressed_filename):
import zopfli.gzip
with open(filename, 'rb') as f_in, \
open(compressed_filename, 'wb') as f_out:
f_out.write(zopfli.gzip.compress(f_in.read()))
def compress(self, path, file):
return ContentFile(zopfli.compress(file.read()))
def compress(data, **kwargs):
"""zlib.compress(data, **kwargs)
""" + zopfli.__COMPRESSOR_DOCSTRING__ + """
Returns:
String containing a zlib container
"""
kwargs['gzip_mode'] = 0
return zopfli.zopfli.compress(data, **kwargs)
def compress(data, *args, **kwargs):
"""gzip.compress(data, **kwargs)
""" + zopfli.__COMPRESSOR_DOCSTRING__ + """
Returns:
String containing a gzip container
"""
kwargs['gzip_mode'] = 1
return zopfli.zopfli.compress(data, *args, **kwargs)
def compress(data, **kwargs):
"""zlib.compress(data, **kwargs)
""" + zopfli.__COMPRESSOR_DOCSTRING__ + """
Returns:
String containing a zlib container
"""
kwargs['gzip_mode'] = 0
return zopfli.zopfli.compress(data, **kwargs)
def compress(data, *args, **kwargs):
"""gzip.compress(data, **kwargs)
""" + zopfli.__COMPRESSOR_DOCSTRING__ + """
Returns:
String containing a gzip container
"""
kwargs['gzip_mode'] = 1
return zopfli.zopfli.compress(data, *args, **kwargs)