This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Django app models to handle image sprites to speed up page loads that have multiple small images.

Project Description
django-sprites is a Django app with two bundled models that handle
Sprites and their associated SpriteItems.

* A Sprite is a combined image, consisting of one or more
SpriteItems, each of which is an image intended to be used as an
image background for an HTML element.

* Combining these background images into a single image can
enormously save on load times. There is a lot of time loss in
sequentially requesting multiple images from the same server, and
only 2-4 can be requested simultaneously. So if you have 20 or
30, that could add hundreds or thousands of milliseconds to your
overall page load.

* Each SpriteItem contains the info actually relevant to your site
design, like the image itself, dimensions (which are calculated),
HTML attributes like class and id, and these are used by the
model methods to generate styling, full CSS lines, and/or an HTML
tag source with the relevant image behind it and any input HTML


* PIL, with libjpeg support

* Django (obviously)

* Uuid

* urllib2


* pip install django-sprites

* Add to your INSTALLED_APPS:

* Also in
MEDIA_ROOT = 'path/to/your/actual/intended/media/root'

This will be used along with the relative paths in the next
config items to determine save locations for images.


This is used by Django to generate URLs for ImageFiles, in
addition to the relative paths below. Fully-qualified path is
not actually required, but it's better to make it fully-
qualified here so that you can use the HTML on any domain, not
just the same as your Django app.

* Also in (optional):
SPRITE_PATH = '<some relative path to store sprites>'
SPRITE_ITEM_PATH = '<another relative path>'

These default to 'sprites' and 'sprite_items', respectively.


* Bulk load images into a sprite by calling:

# or...

for spriteitem in sprite.spriteitem_set.all():
# each spriteitem should get a (unique) css_id at least,
# and a css_class if you intend to do any further styling

* SpriteItem properties
- - outputs CSS style directives, without
specifying it with a selector
i.e.: "display:block; background: url(whatever); etc.;"

- spriteitem.css - outputs the same style directives, with a
selector based on spriteitem.css_id
i.e.: "#myimage {display:block; etc.;}"

- spriteitem.tag_with_style - ouputs a safe-marked (ready to
use in a template) HTML span tag with embedded style attrib,
including any internal HTML given by spriteitem.internal_html

- spriteitem.thumbnail - If created, this is a OneToOne that
links to another SpriteItem, which by convention (but not
as a requirement) is a thumbnail of spriteitem.

- spriteitem.make_thumbnail(width=64,height=64) - return a
PIL.Image copy of spriteitem.image of size width,height.
Aspect ratio is preserved, so size may not match exactly.

This is used by sprite.create_thumbnails(width,height),
which generates new spriteitems, associates those
spriteitems with those of sprite as their thumbnails, and
returns the newly created sprite.

- should be called after making
changes to a sprite. The methods internal to this app handle
this sanely, but if you make changes you'll have to determine
when and how often to re-build your sprites. I'd suggest
building a set of sprites to build first, so you don't

* Middleware
Adding 'sprites.middleware.SpriteReplaceByClass', to
MIDDLEWARE_CLASSES enables the replacement of IMG elements
with styled DIVs. The SRC of the IMG will become the (sprite)
background of the DIV. The DOM translation isn't perfect
going from IMG to DIV, so be careful with this. By default,
IMGs with css class 'sprite_img' will be replaced. The class
can be changed with SPRITE_REPLACE_CSS_CLASS in

Note: The middleware relies on BeautifulSoup. Because one
does not simply regex into HTML.

* Note: Other than the middleware, this isn't really meant to be
used by itself. Ideally, you'll have some kind of model that
you'll ForeignKey relate to SpriteItem, like:

class SiteElement(models.Model):

def get_tag(self):
if is not None:
return '<a href="%s">%s</a>'%(,self.spriteitem.tag_with_style)
return self.spriteitem.tag_with_style

* Should support django-storages backend extensions. Tested
working with S3BotoStorage, at least.

* Detects and sets image filetype based on the PIL-detected format
of the component SpriteItem images. Ultimately it uses the last
SpriteItem attached to the Sprite to set the format.


* Make it split up by image format, maybe. Mixing JPEGs into a
sprite detected as GIF looks atrocious.

* Write tests

* Finish ToDo list
Release History

Release History

This version
History Node


History Node


History Node


History Node


Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-sprites-0.4.0.tar.gz (7.1 kB) Copy SHA256 Checksum SHA256 Source Mar 20, 2012

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting