Skip to main content

Django image renderer

Build Status Coverage PyPI Version

Django image renderer is an app that will help you render images in many sizes (renditions). This can be really helpful for generating images size for different screens resolution (especially when targeting mobile).


Features

  • uses Pillow to resize images

  • uses Django’s default_storage to let you play with whatever storage backend you’ll need

  • uploaded image files named using uuid

  • rendition cached on disk

  • resize keeping original aspect ratio

  • crop if needed

  • simple widget for admin site

Quick start

Install the app.

pip install django-image-renderer

Add “renderer” to your INSTALLED_APPS setting like this:

settings.py

INSTALLED_APPS = (
    # your apps
    'renderer',
)

Include the renderer URL configuration in your project urls.py like this:

urls.py

url(r'^renderer/', include('renderer.urls', namespace='renderer')),

Run python manage.py migrate to create the renderer models.

Start the development server and visit http://localhost:8000/admin/ to create a MasterImage (you’ll need the Admin app enabled).

Requirements

  • Python (2.7, 3.4, 3.5)

  • Django (1.7, 1.8, 1.9)

Usage

There is only one model in the app: MasterImage. With a MasterImage you can ask for renditions.

m = MasterImage.objects.first()

# get the master file's URL
m.get_master_url()
# or
m.get_rendition_url(0, 0)

# cache and return URL of a renditions that as 200 pixels width
# and height computed according to master's aspect ratio
url = m.get_rendition_url(200, 0)

# cache and return URL of a renditions that as 50 pixels height
# and width computed according to master's aspect ratio
url = m.get_rendition_url(0, 50)

If you ask for a size that do not fit master’s aspect ration you’ll receive a center cropped image.

You can also ask for a rendition in templates.

models.py

def index(request):
    m = MasterImage.objects.first()
    return render(request, 'demo/index.html', {
        'master': m,
    })

index.html

{% load renderer %}
...
{% rendition master 42 42 %}
...
{% rendition_url 42 42 %}
...

This will render as:

<img src="/media/img/0fb34de8-9d83-456a-828b-72ab21f8ebab_42x42.png" width="42" height="42" alt="">
...
/media/img/0fb34de8-9d83-456a-828b-72ab21f8ebab_42x42.png
...

When using MasterImage in your model you may need a widget who provides a preview for you image. For convenience a mixin is provided.

models.py

from django.db import models

class DemoModel(models.Model):
    master = models.ForeignKey('renderer.MasterImage')

admin.py

from demo.models import DemoModel
from django.contrib import admin
from renderer.widgets import MasterImageAdminMixin

@admin.register(DemoModel)
class DemoModelAdmin(MasterImageAdminMixin, admin.ModelAdmin):
    fields = ('master', )

Sample project

A sample project is available in the sample folder. Test it as an usual Django project:

virtualenv --no-site-packages venv
source venv/bin/activate
pip install -r requirements.txt
python sample/manage.py migrate
python sample/manage.py createsuperuser
python sample/manage.py runserver

It’ also deployed at http://django-image-renderer.herokuapp.com/.

Release files for django-image-renderer 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-image-renderer 0.1.3
File Size Uploaded
django-image-renderer-0.1.3.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-image-renderer 0.1.3
File Interpreter ABI Platform
django_image_renderer-0.1.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 28.8 kB

Release files / django-image-renderer-0.1.3.tar.gz

Download URL django-image-renderer-0.1.3.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
53522bf93d8b2a8355f7880966997c8a0e11fb9e8fd4d90afde743e6b8be4db9
BLAKE2b-256 checksum
How to use checksums
ab7cb0fa81c70502b2397a11756c650aeb9b22468cba33cb9344ab02a3cc6ac0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_image_renderer-0.1.3-py2.py3-none-any.whl

Download URL django_image_renderer-0.1.3-py2.py3-none-any.whl
Size 16.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8f4419e96b2ed63297c3f50486adf4a0cd65d92d736901a54e00ab93d614a4de
BLAKE2b-256 checksum
How to use checksums
bb93d545d38e2fae57c66c9c9b63652d25524bf2e56a754e207c7ab6a4dc1f31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page