Package: django-sprites
django-sprites is a Django app with two bundled models that handleSprites and their associated SpriteItems.-----------* A Sprite is a combined image, consisting of one or moreSpriteItems, each of which is an image intended to be used as animage background for an HTML element.* Combining these background images into a single image canenormously save on load times.  There is a lot of time loss insequentially requesting multiple images from the same server, andonly 2-4 can be requested simultaneously.  So if you have 20 or30, that could add hundreds or thousands of milliseconds to youroverall page load.* Each SpriteItem contains the info actually relevant to your sitedesign, like the image itself, dimensions (which are calculated),HTML attributes like class and id, and these are used by themodel methods to generate styling, full CSS lines, and/or an HTMLtag source with the relevant image behind it and any input HTMLinside.Requirements:-----------*  PIL, with libjpeg support*  Django (obviously)*  Uuid*  urllib2Setup:-----------*  pip install django-sprites*  Add to your settings.py INSTALLED_APPS:'sprites',*  Also in settings.py:MEDIA_ROOT = 'path/to/your/actual/intended/media/root'This will be used along with the relative paths in the nextconfig items to determine save locations for images.MEDIA_URL = 'http://myserver.mydomain.com/media'This is used by Django to generate URLs for ImageFiles, inaddition to the relative paths below.  Fully-qualified path isnot actually required, but it's better to make it fully-qualified here so that you can use the HTML on any domain, notjust the same as your Django app.*  Also in settings.py (optional):SPRITE_PATH = '<some relative path to store sprites>'SPRITE_ITEM_PATH = '<another relative path>'These default to 'sprites' and 'sprite_items', respectively.Usage:-----------*  Bulk load images into a sprite by calling:sprite=Sprite.create_from_urls(['http://path.to/first.jpg','http://path.to/second.jpg'...])# or...sprite=Sprite.create_from_local_files(['/path/to/first/file','/path/to/second/file'])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 stylingpass*  SpriteItem properties- spriteitem.style - outputs CSS style directives, withoutspecifying it with a selectori.e.: "display:block; background: url(whatever); etc.;"- spriteitem.css - outputs the same style directives, with aselector based on spriteitem.css_idi.e.: "#myimage {display:block; etc.;}"- spriteitem.tag_with_style - ouputs a safe-marked (ready touse 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 thatlinks to another SpriteItem, which by convention (but notas a requirement) is a thumbnail of spriteitem.- spriteitem.make_thumbnail(width=64,height=64) - return aPIL.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 thosespriteitems with those of sprite as their thumbnails, andreturns the newly created sprite.- spriteitem.sprite.build() should be called after makingchanges to a sprite.  The methods internal to this app handlethis sanely, but if you make changes you'll have to determinewhen and how often to re-build your sprites.  I'd suggestbuilding a set of sprites to build first, so you don't*  MiddlewareAdding 'sprites.middleware.SpriteReplaceByClass', toMIDDLEWARE_CLASSES enables the replacement of IMG elementswith styled DIVs.  The SRC of the IMG will become the (sprite)background of the DIV.  The DOM translation isn't perfectgoing from IMG to DIV, so be careful with this.  By default,IMGs with css class 'sprite_img' will be replaced.  The classcan be changed with SPRITE_REPLACE_CSS_CLASS in settings.py.Note:  The middleware relies on BeautifulSoup. Because onedoes not simply regex into HTML.*  Note:  Other than the middleware, this isn't really meant to beused by itself.  Ideally, you'll have some kind of model thatyou'll ForeignKey relate to SpriteItem, like:class SiteElement(models.Model):spriteitem=models.ForeignKey(SpriteItem)link=models.CharField(max_length=255,null=True,blank=True)def get_tag(self):if self.link is not None:return '<a href="%s">%s</a>'%(self.link,self.spriteitem.tag_with_style)return self.spriteitem.tag_with_style*  Should support django-storages backend extensions.  Testedworking with S3BotoStorage, at least.*  Detects and sets image filetype based on the PIL-detected formatof the component SpriteItem images.  Ultimately it uses the lastSpriteItem attached to the Sprite to set the format.ToDo:-----------*  Make it split up by image format, maybe.  Mixing JPEGs into asprite detected as GIF looks atrocious.*  Write tests*  Finish ToDo list