Optimizers

heynemann edited this page Oct 4, 2014 · 1 revision

Optimizers are programs that take the image buffer and replace it with a new one in the end of thumbor's processing.

This feature is meant to support usage of advanced image compressors like jpegtran or pngquant.

Using optimizers

Using optimizers is really simple. You just need to add them to the OPTIMIZERS configuration in your config file:

OPTIMIZERS = [
    'thumbor.optimizers.jpegtran'
]

If you are using the jpegtran optimizer that comes with thumbor, you also need to specify the path where thumbor can find the jpegtran binary:

JPEGTRAN_PATH = '/usr/bin/jpegtran'

Creating optimizers

To create a new optimizer, inherit from thumbor.optimizer.BaseOptimizer and implement the optimize method. The jpegtran optimizer implementation looks like the following:

def optimize(self, buffer, input_file, output_file):
    jpegtran_path = self.context.config.JPEGTRAN_PATH
    command = '%s -copy all -optimize -progressive -outfile %s %s ' % (
        jpegtran_path,
        output_file.name,
        input_file.name,
    )
    os.system(command)

In order to specify whether the optimizer should be run for the current image, you should implement the should_run method. Again the jpegtran optimizer looks like the following:

def should_run(self, image_extension, buffer):
    return 'jpg' in image_extension or 'jpeg' in image_extension