0

Merge pull request #1163 from CounterPillow/optimizeimg-fixes

Add jpegoptim interface to optimizeimages
This commit is contained in:
Andrew Chin
2014-11-25 02:17:14 -05:00
2 changed files with 66 additions and 2 deletions

View File

@@ -683,8 +683,8 @@ Image options
Using image optimizers will increase render times significantly.
This option specifies which additional tools overviewer should use to
optimize the filesize of png tiles.
The tools used must be placed somewhere, where overviewer can find them, for
optimize the filesize of rendered tiles.
The tools used must be placed somewhere where overviewer can find them, for
example the "PATH" environment variable or a directory like /usr/bin.
The option is a list of Optimizer objects, which are then executed in
@@ -766,6 +766,34 @@ Image options
**Default:** ``False``
``jpegoptim``
jpegoptim can do both lossy and lossless JPEG optimisation. If no options are specified,
jpegoptim will only do lossless optimisations.
Available settings:
``quality``
A number between 0 and 100 that corresponds to the jpeg quality level. If the input
image has a lower quality specified than the output image, jpegoptim will only do
lossless optimisations.
If this option is specified and the above condition does not apply, jpegoptim will
do lossy optimisation.
**Default:** ``None`` *(= Unspecified)*
``target_size``
Either a percentage of the original filesize (e.g. ``"50%"``) or a target filesize
in kilobytes (e.g. ``15``). jpegoptim will then try to reach this as its target size.
If specified, jpegoptim will do lossy optimisation.
.. warning::
This appears to have a greater performance impact than just setting ``quality``.
Unless predictable filesizes are a thing you need, you should probably use ``quality``
instead.
**Default:** ``None`` *(= Unspecified)*
**Default:** ``[]``
Zoom

View File

@@ -121,6 +121,42 @@ class optipng(Optimizer, PNGOptimizer):
def is_crusher(self):
return True
class jpegoptim(Optimizer, JPEGOptimizer):
binaryname = "jpegoptim"
crusher = True
quality = None
target_size = None
def __init__(self, quality = None, target_size = None):
if quality is not None:
if quality < 0 or quality > 100:
raise Exception("Invalid target quality %d for jpegoptim" % quality)
self.quality = quality
if target_size is not None:
self.target_size = target_size
def optimize(self, img):
args = [self.binaryname, "-q", "-p"]
if self.quality is not None:
args.append("-m" + str(self.quality))
if self.target_size is not None:
args.append("-S" + str(self.target_size))
args.append(img)
Optimizer.fire_and_forget(self, args)
def is_crusher(self):
# Technically, optimisation is lossless if input image quality
# is below target quality, but this is irrelevant in this case
if (self.quality is not None) or (self.target_size is not None):
return False
else:
return True
def optimize_image(imgpath, imgformat, optimizers):
for opt in optimizers: