mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 09:56:17 +03:00
Merge pull request #2129 from radarhere/truth
Use truth value when checking for optimize option on save
This commit is contained in:
commit
2401150ce9
|
@ -688,12 +688,14 @@ def _save(im, fp, filename):
|
||||||
progressive = info.get("progressive", False) or\
|
progressive = info.get("progressive", False) or\
|
||||||
info.get("progression", False)
|
info.get("progression", False)
|
||||||
|
|
||||||
|
optimize = info.get("optimize", False)
|
||||||
|
|
||||||
# get keyword arguments
|
# get keyword arguments
|
||||||
im.encoderconfig = (
|
im.encoderconfig = (
|
||||||
quality,
|
quality,
|
||||||
progressive,
|
progressive,
|
||||||
info.get("smooth", 0),
|
info.get("smooth", 0),
|
||||||
"optimize" in info,
|
optimize,
|
||||||
info.get("streamtype", 0),
|
info.get("streamtype", 0),
|
||||||
dpi[0], dpi[1],
|
dpi[0], dpi[1],
|
||||||
subsampling,
|
subsampling,
|
||||||
|
@ -707,7 +709,7 @@ def _save(im, fp, filename):
|
||||||
# channels*size, this is a value that's been used in a django patch.
|
# channels*size, this is a value that's been used in a django patch.
|
||||||
# https://github.com/matthewwithanm/django-imagekit/issues/50
|
# https://github.com/matthewwithanm/django-imagekit/issues/50
|
||||||
bufsize = 0
|
bufsize = 0
|
||||||
if "optimize" in info or progressive:
|
if optimize or progressive:
|
||||||
# keep sets quality to 0, but the actual value may be high.
|
# keep sets quality to 0, but the actual value may be high.
|
||||||
if quality >= 95 or quality == 0:
|
if quality >= 95 or quality == 0:
|
||||||
bufsize = 2 * im.size[0] * im.size[1]
|
bufsize = 2 * im.size[0] * im.size[1]
|
||||||
|
|
|
@ -696,15 +696,10 @@ def _save(im, fp, filename, chunk=putchunk, check=0):
|
||||||
mode = "%s;%d" % (mode, bits)
|
mode = "%s;%d" % (mode, bits)
|
||||||
|
|
||||||
# encoder options
|
# encoder options
|
||||||
if "dictionary" in im.encoderinfo:
|
im.encoderconfig = (im.encoderinfo.get("optimize", False),
|
||||||
dictionary = im.encoderinfo["dictionary"]
|
|
||||||
else:
|
|
||||||
dictionary = b""
|
|
||||||
|
|
||||||
im.encoderconfig = ("optimize" in im.encoderinfo,
|
|
||||||
im.encoderinfo.get("compress_level", -1),
|
im.encoderinfo.get("compress_level", -1),
|
||||||
im.encoderinfo.get("compress_type", -1),
|
im.encoderinfo.get("compress_type", -1),
|
||||||
dictionary)
|
im.encoderinfo.get("dictionary", b""))
|
||||||
|
|
||||||
# get the corresponding PNG mode
|
# get the corresponding PNG mode
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -126,9 +126,12 @@ class TestFileJpeg(PillowTestCase):
|
||||||
|
|
||||||
def test_optimize(self):
|
def test_optimize(self):
|
||||||
im1 = self.roundtrip(hopper())
|
im1 = self.roundtrip(hopper())
|
||||||
im2 = self.roundtrip(hopper(), optimize=1)
|
im2 = self.roundtrip(hopper(), optimize=0)
|
||||||
|
im3 = self.roundtrip(hopper(), optimize=1)
|
||||||
self.assert_image_equal(im1, im2)
|
self.assert_image_equal(im1, im2)
|
||||||
|
self.assert_image_equal(im1, im3)
|
||||||
self.assertGreaterEqual(im1.bytes, im2.bytes)
|
self.assertGreaterEqual(im1.bytes, im2.bytes)
|
||||||
|
self.assertGreaterEqual(im1.bytes, im3.bytes)
|
||||||
|
|
||||||
def test_optimize_large_buffer(self):
|
def test_optimize_large_buffer(self):
|
||||||
# https://github.com/python-pillow/Pillow/issues/148
|
# https://github.com/python-pillow/Pillow/issues/148
|
||||||
|
@ -139,9 +142,14 @@ class TestFileJpeg(PillowTestCase):
|
||||||
|
|
||||||
def test_progressive(self):
|
def test_progressive(self):
|
||||||
im1 = self.roundtrip(hopper())
|
im1 = self.roundtrip(hopper())
|
||||||
im2 = self.roundtrip(hopper(), progressive=True)
|
im2 = self.roundtrip(hopper(), progressive=False)
|
||||||
self.assert_image_equal(im1, im2)
|
im3 = self.roundtrip(hopper(), progressive=True)
|
||||||
self.assertGreaterEqual(im1.bytes, im2.bytes)
|
self.assertFalse(im1.info.get("progressive"))
|
||||||
|
self.assertFalse(im2.info.get("progressive"))
|
||||||
|
self.assertTrue(im3.info.get("progressive"))
|
||||||
|
|
||||||
|
self.assert_image_equal(im1, im3)
|
||||||
|
self.assertGreaterEqual(im1.bytes, im3.bytes)
|
||||||
|
|
||||||
def test_progressive_large_buffer(self):
|
def test_progressive_large_buffer(self):
|
||||||
f = self.tempfile('temp.jpg')
|
f = self.tempfile('temp.jpg')
|
||||||
|
|
|
@ -219,20 +219,20 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options:
|
||||||
image quality.
|
image quality.
|
||||||
|
|
||||||
**optimize**
|
**optimize**
|
||||||
If present, indicates that the encoder should make an extra pass over the
|
If present and true, indicates that the encoder should make an extra pass
|
||||||
image in order to select optimal encoder settings.
|
over the image in order to select optimal encoder settings.
|
||||||
|
|
||||||
**progressive**
|
**progressive**
|
||||||
If present, indicates that this image should be stored as a progressive
|
If present and true, indicates that this image should be stored as a
|
||||||
JPEG file.
|
progressive JPEG file.
|
||||||
|
|
||||||
**dpi**
|
**dpi**
|
||||||
A tuple of integers representing the pixel density, ``(x,y)``.
|
A tuple of integers representing the pixel density, ``(x,y)``.
|
||||||
|
|
||||||
**icc_profile**
|
**icc_profile**
|
||||||
If present, the image is stored with the provided ICC profile. If
|
If present and true, the image is stored with the provided ICC profile.
|
||||||
this parameter is not provided, the image will be saved with no
|
If this parameter is not provided, the image will be saved with no profile
|
||||||
profile attached. To preserve the existing profile::
|
attached. To preserve the existing profile::
|
||||||
|
|
||||||
im.save(filename, 'jpeg', icc_profile=im.info.get('icc_profile'))
|
im.save(filename, 'jpeg', icc_profile=im.info.get('icc_profile'))
|
||||||
|
|
||||||
|
@ -399,9 +399,9 @@ chunks is limited to ``PngImagePlugin.MAX_TEXT_MEMORY``, defaulting to
|
||||||
The :py:meth:`~PIL.Image.Image.save` method supports the following options:
|
The :py:meth:`~PIL.Image.Image.save` method supports the following options:
|
||||||
|
|
||||||
**optimize**
|
**optimize**
|
||||||
If present, instructs the PNG writer to make the output file as small as
|
If present and true, instructs the PNG writer to make the output file as
|
||||||
possible. This includes extra processing in order to find optimal encoder
|
small as possible. This includes extra processing in order to find optimal
|
||||||
settings.
|
encoder settings.
|
||||||
|
|
||||||
**transparency**
|
**transparency**
|
||||||
For ``P``, ``L``, and ``RGB`` images, this option controls what
|
For ``P``, ``L``, and ``RGB`` images, this option controls what
|
||||||
|
@ -615,8 +615,7 @@ format are currently undocumented.
|
||||||
The :py:meth:`~PIL.Image.Image.save` method supports the following options:
|
The :py:meth:`~PIL.Image.Image.save` method supports the following options:
|
||||||
|
|
||||||
**lossless**
|
**lossless**
|
||||||
If present, instructs the WEBP writer to use lossless
|
If present and true, instructs the WEBP writer to use lossless compression.
|
||||||
compression.
|
|
||||||
|
|
||||||
**quality**
|
**quality**
|
||||||
Integer, 1-100, Defaults to 80. Sets the quality level for
|
Integer, 1-100, Defaults to 80. Sets the quality level for
|
||||||
|
|
Loading…
Reference in New Issue
Block a user