django-dynamic-sprites

A way to generate sprites based on objects created by the application user
Download

django-dynamic-sprites Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Vinicius Mendes
  • Publisher web site:
  • http://github.com/vbmendes/

django-dynamic-sprites Tags


django-dynamic-sprites Description

django-dynamic-sprites is a Django app to create sprites dynamically for Python and Django.InstalationInstall the package via pip:pip install django-dynamic-spritesGenerating sprite for images in a folderOne way to generate sprites is from all pictures within a folder. To do so, type this command:generate_sprites.py path/to/folder path/to/outputOne thing to notice is that you don't pass the output extension. The script already generates the image with .png and the CSS with .css.Generating sprite from Python codeWithin your python code you can generate sprites for a given set of images. All you have to do is provide the images paths, a nome to each image, generate the sprite and save it:from dynamic_sprites.sprite import Spriteimages = ( ('brazil', '/path/to/brazil/image.png'), ('usa', '/path/to/usa/image.png'),)sprite = Sprite('sprite_name', images)output_image = sprite.generate()output_image.save('/path/to/output/image.png')output_css = sprite.generate_css('http://images.com/output/image.png')output_css.save('/path/to/output/style.css')That's the basics for generating a sprite from Python code. But there is some abstractions integrating it with Django. Even the name of the project having Django on it, the sprites can be generated without using Django.Generating sprites for Django queryset objectsLet's pretend you have a Django model like this:from django.db import modelsclass Country(models.Model): name = models.CharField(max_length=255) slug = models.SlugField() flag = models.ImageField(upload_to='countries')And you want to have a sprite with all the country flags. You can generate it using a Sprite specialization:from dynamic_sprites.model_sprite import ModelSpritesprite = ModelSprite('country-flags', queryset=Country.objects.all(), image_field='flag', slug_field='slug')output_image = sprite.generate()output_image.save('/path/to/output/image.png')output_css = sprite.generate_css('http://images.com/output/image.png')output_css.save('/path/to/output/style.css')You can also connect the sprite generation to the post_save listener and have your sprite generated again each time an object in your queryset is saved:from django.db.models.signals import post_savefrom dynamic_sprites.listeners import ModelSpriteListenerlistener = ModelSpriteListener('country-flags', image_field='flag', slug_field='slug', queryset=Country.objects.all())post_save.connect(listener, sender=Country)Contributing to the projectThis project is open source, contributions are welcome.Product's homepage


django-dynamic-sprites Related Software