Image Processors

Easy thumbnails generates thumbnail images by passing the source image through a series of image processors. Each processor may alter the image, often dependent on the options it receives.

This makes the system very flexible, as the processors an image passes through can be defined in THUMBNAIL_PROCESSORS and even overridden by an individual easy_thumbnails.files.Thumbnailer (via the thumbnail_processors attribute).

Built-in processors

Following is a list of the built-in processors, along with the thumbnail options which they use.

easy_thumbnails.processors.autocrop(im, autocrop=False, **kwargs)

Remove any unnecessary whitespace from the edges of the source image.

This processor should be listed before scale_and_crop() so the whitespace is removed from the source image before it is resized.

autocrop
Activates the autocrop method for this image.
easy_thumbnails.processors.background(im, size, background=None, crop=None, replace_alpha=None, **kwargs)

Add borders of a certain color to make the resized image fit exactly within the dimensions given.

background
Background color to use
easy_thumbnails.processors.colorspace(im, bw=False, replace_alpha=False, **kwargs)

Convert images to the correct color space.

A passive option (i.e. always processed) of this method is that all images (unless grayscale) are converted to RGB colorspace.

This processor should be listed before scale_and_crop() so palette is changed before the image is resized.

bw
Make the thumbnail grayscale (not really just black & white).
replace_alpha
Replace any transparency layer with a solid color. For example, replace_alpha='#fff' would replace the transparency layer with white.
easy_thumbnails.processors.filters(im, detail=False, sharpen=False, **kwargs)

Pass the source image through post-processing filters.

sharpen
Sharpen the thumbnail image (using the PIL sharpen filter)
detail
Add detail to the image, like a mild sharpen (using the PIL detail filter).
easy_thumbnails.processors.scale_and_crop(im, size, crop=False, upscale=False, zoom=None, target=None, **kwargs)

Handle scaling and cropping the source image.

Images can be scaled / cropped against a single dimension by using zero as the placeholder in the size. For example, size=(100, 0) will cause the image to be resized to 100 pixels wide, keeping the aspect ratio of the source image.

crop

Crop the source image height or width to exactly match the requested thumbnail size (the default is to proportionally resize the source image to fit within the requested thumbnail size).

By default, the image is centered before being cropped. To crop from the edges, pass a comma separated string containing the x and y percentage offsets (negative values go from the right/bottom). Some examples follow:

  • crop="0,0" will crop from the left and top edges.
  • crop="-10,-0" will crop from the right edge (with a 10% offset) and the bottom edge.
  • crop=",0" will keep the default behavior for the x axis (horizontally centering the image) and crop from the top edge.

The image can also be “smart cropped” by using crop="smart". The image is incrementally cropped down to the requested size by removing slices from edges with the least entropy.

Finally, you can use crop="scale" to simply scale the image so that at least one dimension fits within the size dimensions given (you may want to use the upscale option too).

upscale
Allow upscaling of the source image during scaling.
zoom
A percentage to zoom in on the scaled image. For example, a zoom of 40 will clip 20% off each side of the source image before thumbnailing.
target

Set the focal point as a percentage for the image if it needs to be cropped (defaults to (50, 50)).

For example, target="10,20" will set the focal point as 10% and 20% from the left and top of the image, respectively. If the image needs to be cropped, it will trim off the right and bottom edges until the focal point is centered.

Can either be set as a two-item tuple such as (20, 30) or a comma separated string such as "20,10".

A null value such as (20, None) or ",60" will default to 50%.

Custom processors

You can replace or leave out any default processor as suits your needs. Following is an explanation of how to create and activate a custom processor.

When defining the THUMBNAIL_PROCESSORS setting, remember that this is the order through which the processors are run. The image received by a processor is the output of the previous processor.

Create the processor

First create a processor like this:

def whizzbang_processor(image, bang=False, **kwargs):
    """
    Whizz bang the source image.

    """
    if bang:
        image = whizz(image)
    return image

The first argument for a processor is the source image.

All other arguments are keyword arguments which relate to the list of options received by the thumbnail generator (including size and quality). Ensure you list all arguments which could be used (giving them a default value of False), as the processors arguments are introspected to generate a list of valid options.

You must also use **kwargs at the end of your argument list because all options used to generate the thumbnail are passed to processors, not just the ones defined.

Whether a processor actually modifies the image or not, they must always return an image.

Use the processor

Next, add the processor to THUMBNAIL_PROCESSORS in your settings module:

from easy_thumbnails.conf import Settings as easy_thumbnails_defaults

THUMBNAIL_PROCESSORS = easy_thumbnails_defaults.THUMBNAIL_PROCESSORS + (
    'wb_project.thumbnail_processors.whizzbang_processor',
)