render image in various sizes
Project description
Django image renderer is Django 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 orignal aspect ratio
crop if needed
simple widget for admin site
Quick start
Add “renderer” to your INSTALLED_APPS setting like this:
INSTALLED_APPS = (
# your apps
'renderer',
)
Include the renderer URLconf in your project urls.py like this:
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.2, 3.3, 3.4, 3.5)
Django (1.7, 1.8, 1.9b1)
Installation
pip install django-image-renderer
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.
def index(request):
m = MasterImage.objects.first()
return render(request, 'demo/index.html', {
'master': m,
})
{% load renderer %}
...
{% rendition master 42 42 %}
...
This will render as:
<img src="/media/img/0fb34de8-9d83-456a-828b-72ab21f8ebab_42x42.png" width="42" height="42" alt="">
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
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
Built Distribution
Hashes for django-image-renderer-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38ec40efd79895bca7ea296e3f7249cdf49110eaef49296e1c9547b4405a6fe3 |
|
MD5 | 19e9748daba433b1624bdbefb02ca6bb |
|
BLAKE2b-256 | 634eb47d74f352143d7a48d70fd51aaa9a1ac5e76f295a39afdafd6e1d4960dd |
Hashes for django_image_renderer-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2eeb3788f9889ae7adcce75f059f27dd6878ce77d2a24393f114f105616b515d |
|
MD5 | f4d7e70c1d1e1909427536b72d78723d |
|
BLAKE2b-256 | 9661f093a2b0a5366f0712256274e6bfd2aee03eebcbdc0669b42c5416defe1d |