Adjustments

Daguerre provides a variety of adjustments to use when processing images, as well as an API for registering custom adjustments.

class daguerre.adjustments.Adjustment(**kwargs)

Base class for all adjustments which can be carried out on an image. The adjustment itself represents a set of parameters, which can then be applied to images (taking areas into account if applicable).

Adjustment subclasses need to define two methods: calculate() and adjust(). If the method doesn’t use areas, you can set the uses_areas attribute on the method to False to optimize adjustment.

Parameters:kwargs – The requested kwargs for the adjustment. The keys must be in parameters or the adjustment is invalid.
adjust(image, areas=None)

Manipulates and returns the image. Must be implemented by subclasses.

Parameters:
  • image – PIL Image which will be adjusted.
  • areas – iterable of Area instances to be considered in performing the adjustment.
calculate(dims, areas=None)

Calculates the dimensions of the adjusted image without actually manipulating the image. By default, just returns the given dimensions.

Parameters:
  • dims(width, height) tuple of the current image dimensions.
  • areas – iterable of Area instances to be considered in calculating the adjustment.
parameters = ()

Accepted parameters for this adjustment - for example, "width", "height", "color", "unicorns", etc.

Built-In Adjustments

class daguerre.adjustments.Fit(**kwargs)

Resizes an image to fit entirely within the given dimensions without cropping and maintaining the width/height ratio.

If neither width nor height is specified, this adjustment will simply return a copy of the image.

parameters = ('width', 'height')
class daguerre.adjustments.Fill(**kwargs)

Crops the image to the requested ratio (using the same logic as Crop to protect Area instances which are passed in), then resizes it to the actual requested dimensions. If width or height is not given, then the unspecified dimension will be allowed to expand up to max_width or max_height, respectively.

parameters = ('width', 'height', 'max_width', 'max_height')
class daguerre.adjustments.Crop(**kwargs)

Crops an image to the given width and height, without scaling it. Area instances which are passed in will be protected as much as possible during the crop.

parameters = ('width', 'height')
class daguerre.adjustments.RatioCrop(**kwargs)

Crops an image to the given aspect ratio, without scaling it. Area instances which are passed in will be protected as much as possible during the crop.

parameters = ('ratio',)

ratio should be formatted as "<width>:<height>"

class daguerre.adjustments.NamedCrop(**kwargs)

Crops an image to the given named area, without scaling it. Area instances which are passed in will be protected as much as possible during the crop.

If no area with the given name exists, this adjustment is a no-op.

parameters = ('name',)

When used with the template tag, these adjustments should be referred to by their lowercase name:

{% adjust image "fit" width=300 %}

See Using daguerre for examples.

Custom Adjustments

You can easily add custom adjustments for your particular project. For example, an adjustment to make an image grayscale might look something like this:

# Somewhere that will be imported.
from daguerre.adjustments import Adjustment, registry
from PIL import ImageOps

@registry.register
class GrayScale(Adjustment):
    def adjust(self, image, areas=None):
        return ImageOps.grayscale(image)
    adjust.uses_areas = False

Now you can use your adjustment in templates:

{% adjust image "grayscale" %}