djangoimagekit

Imagekit SpecFields to Scale down images, but not scale up


Imagekit will scale down uploaded images larger than 800x500, however it will also scale up images smaller than 800x500.

How can a ImageSpecField be created so that images which fit well within the defined size are not scaled up?

post_embed_image = ImageSpecField([SmartCrop(800, 500)], image_field='original_image')
post_embed_resized_image = ImageSpecField([SmartResize(800, 500)], image_field='original_image')

Solution

  • It turns how the imagekit docs are outdated and have no mention of the ResizeToFit processor with the Upscale boolean

    https://github.com/jdriscoll/django-imagekit/blob/master/imagekit/processors/resize.py

    The following ImageSpecField will resize large images and preserve smaller images.

    post_resized_image = ImageSpecField([ResizeToFit(800, 500, False)], image_field='original_image')