Skip to main content

Image field for Django

Project description

https://travis-ci.org/matthiask/django-imagefield.svg?branch=master Documentation Status

Heavily based on django-versatileimagefield, but with a few important differences:

  • The amount of code is kept at a minimum. At the time of writing django-versatileimagefield has 4-5 times as much code (without tests).

  • Generating images on-demand inside rendering code is made hard on purpose. Instead, images are generated when models are saved and also by running the management command process_all_imagefields.

  • django-imagefield does not depend on a fast storage or a cache to be and stay fast.

Replacing existing uses of django-versatileimagefield requires the following steps:

  • from imagefield.fields import ImageField as VersatileImageField, PPOIField

  • Specify the image sizes by either providing ImageField(formats=...) or adding the IMAGEFIELD_FORMATS setting.

  • Convert template code to access the new properties.

  • When using django-imagefield with a PPOI, make sure that the PPOI field is also added to ModelAdmin or InlineModelAdmin fieldsets, otherwise you’ll just see the image, but no PPOI picker.

If you used e.g. instance.image.crop.200x200 and instance.image.thumbnail.800x500 before, you should add the following setting:

IMAGEFIELD_FORMATS = {
    # image field path, lowercase
    'yourapp.yourmodel.image': {
        'square': ['default', ('crop', (200, 200))],
        'full': ['default', ('thumbnail', (800, 500))],
    },
}

After running ./manage.py process_all_imagefields once you can now use use instance.image.square and instance.image.thumbnail in templates instead. Note that the properties on the image file do by design not check whether thumbs exist.

Image processors

django-imagefield uses an image processing pipeline modelled after Django’s middleware.

The following processors are available out of the box:

  • autorotate: Autorotates an image by reading the EXIF data.

  • process_jpeg: Converts non-RGB images to RGB, activates progressive encoding and sets quality to a higher value of 90.

  • process_gif: Preserves transparency and palette data in resized images.

  • preserve_icc_profile: As the name says.

  • thumbnail: Resizes images to fit a bounding box.

  • crop: Crops an image to the given dimensions, also takes the PPOI (primary point of interest) information into account if provided.

  • default: The combination of autorotate, process_jpeg, process_gif and preserve_icc_profile. Additional default processors may be added in the future. It is recommended to use default instead of adding the processors one-by-one.

Processors can be specified either using their name alone, or if they take arguments, using a tuple (processor_name, args...).

You can easily register your own processors or even override built-in processors if you want to:

from imagefield.processing import register

# You could also write a class with a __call__ method, but I really
# like the simplicity of functions.

@register
def my_processor(get_image, args):
    # args is either a list of arguments to the processor or an
    # empty list
    def processor(image, context):
        # read some information from the image...
        # or maybe modify it, but it's mostly recommended to modify
        # the image after calling get_image

        image, context = get_image(image, context)

        # modify the image, and return it...
        modified_image = ...
        # maybe modify the context...
        return modified_image, context
    return processor

The processor’s name is taken directly from the registered object.

The context is a types.SimpleNamespace containing the following variables (but feel free to add your own):

  • ppoi: The primary point of interest as a list of two floats between 0 and 1.

  • save_kwargs: A dictionary of keyword arguments to pass to PIL.Image.save.

Development

django-imagefield uses both flake8 and isort to check for style violations. It is recommended to add the following git hook as an executable file at .git/hooks/pre-commit:

#!/bin/bash
set -ex
export PYTHONWARNINGS=ignore
tox -e style

The easiest way to build the documentation and run the test suite is using tox:

tox -e docs  # Open docs/build/html/index.html
tox -e tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-imagefield-0.0.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

django_imagefield-0.0.1-py2.py3-none-any.whl (14.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-imagefield-0.0.1.tar.gz.

File metadata

File hashes

Hashes for django-imagefield-0.0.1.tar.gz
Algorithm Hash digest
SHA256 fd795eedacfdf516d2d20f15863ab2b1b3af841a1c033f2294e82a0ccb788e4f
MD5 b5002e4cdc1de1175ce676171f3bc959
BLAKE2b-256 9d9710ef106945deef1c8b6ff914f62539bbc5b2a900f1f490fc2059ad6fb019

See more details on using hashes here.

File details

Details for the file django_imagefield-0.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_imagefield-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3ab385bee3b3af9820f867887e7df6029a9e78ea43a968c8987f25a860333574
MD5 2b3bc46aa02196a07d3e87e834534e52
BLAKE2b-256 5ff713f8b3099e90d37c9014e19e9af994c70fcde237d54788a49c4b0b9d75a8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page