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).
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.
- 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 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.
- Make the thumbnail grayscale (not really just black & white).
- 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 the thumbnail image (using the PIL sharpen filter)
- 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, **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 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).
- Allow upscaling of the source image during scaling.
- 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.
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.