Processors

Processors hold the implementation details of how to transform or preview files.

They are called by transformers with a source file and are expected to produce an output after applying transformations.

Check out the interface for processors in BaseProcessor and an implementation example in PillowProcessor.

class anchor.services.processors.BaseProcessor

Bases: object

Interface for file processors.

A file processor must implement the source() method and save() methods outlined below at the minimum.

For convenience, the source() method returns the processor itself, so you can chain methods together. Check out the PillowProcessor for an example implementation.

save(file, format: str)

Save the processed file to the given file-like object.

The file passed is a file-like object that responds to write, like a TemporaryFile or an io stream.

The format is a string representing the file format to save the file as, like 'jpeg' or 'png'.

source(file)

Set up the processor to work with the given file.

The file passed is a file-like object that responds to read, like a DjangoFile or an io stream.

class anchor.services.processors.PillowProcessor

Bases: BaseProcessor

A file processor that uses the Pillow library to transform images.

To use this processor, make sure to install the Pillow library: pip install Pillow.

resize_to_fit(width: int, height: int)

Resize the image to fit within the given width and height, preserving aspect ratio.

Aspect ratio is maintained, so the final image dimensions may be smaller than the provided rectangle. If the image is smaller than the provided dimensions, it will be upscaled.

resize_to_limit(width: int, height: int)

Downsize the image to fit within the given width and height, preserving aspect ratio.

If the image is already smaller than the provided dimensions, nothing is done.

rotate(degrees: int)

Rotates the image by the given angle.

save(file, format: str)

Save the processed file to the given file-like object.

The file passed is a file-like object that responds to write, like a TemporaryFile or an io stream.

The format is a string representing the file format to save the file as, like 'jpeg' or 'png'.

source(file)

Set up the processor to work with the given file.

The file passed is a file-like object that responds to read, like a DjangoFile or an io stream.