Dynamic Image Replacements in Django

Monday, 6th October 2008

There are a few different ways to use custom fonts on the web such as the various versions of sIFR which are better than others since they don’t dependent on any server side language, but the con of course is that it adds extra javascript and flash bloat to websites.

While messing around PIL and not finding any Python/Django equivalents of Facelift, which generates the images through php rather than flash - I saw this as a good opportunity to get more into both PIL and custom template filters.

Django Image Replacement

Django Image Replacement is a template filter which generate image replacements for header elements such as <h2> and <h3>. But rather than replacing all elements with javascript you do it right in your Django templates.

Quick Example:

{% load image_replacement %}

<h2>{% filter title|image_replacement:"myfont.ttf, 30, #555" %}</h2>
# or
<h2>{% filter image_replacement:"myfont.ttf, 30, #555" %}Django Image Replacement{% endfilter %}</h2>

Check out the example page to see how these would be rendered.

Update: You can now also use the image_replacement_path filter to get just replacement image path.

{% filter title|image_replacement:"myfont.ttf, 30, #555" %}
# .. would return, in my case:
# "/static/image_replacement/images/IzU1NURqYW5nbyBJbWFnZSBSZXBsYWNlbWVudENhcHR1cmVfaXQudHRmMzA=.png"

How it works

Now, what is actually going on here? First of all we apply the filter using |filter to any variable we might have, or we can just use the block {% filter %} tag to replace anything static. image_replacement takes three comma separated arguments:

  • font-style, which can either be a TrueType (.ttf) or OpenType (.otf) font which are placed in /image_replacement/fonts/ directory, which is created wherever your MEDIA_ROOT lives.
  • font-size, that can be any size integer.
  • and font-color, which can either be a spelled out color such as red or a hex such as #ff0000

Each of these three are required.

The image is saved in /image_replacement/images/ so that they aren’t created each time the page loads, each image is only created once but if you change any parameter or the content it will create a new one.

Requirements

Fonts is where you would place your fonts that you’d like to use and images is where the replacements are saved. You must also place the file clear.png in /image_replacement/images/.

If you are planning to use this on, let’s say, <h2> elements, you must add

h2, h2 span { text-indent: -999em; height: 30px; }

to your CSS file (so the default font won’t overlap the replacement), where height would be the height of the replacement image, which varies depending on the font used.

Known Issues

  • PIL does not allow you to set a box boundary, so text-wrapping is not supported at the moment.

Download

Django Image Replacement is hosted over at Google Code, so head on over there to get the latest version.

Thanks

I would love some input on how to improve this, but I’d also like to say that this is meant as a replacement for anything - just an alternative.

Currently Django Image Replacement focuses on easy of use rather than clean generated html, so you’ll have to bare with that at the moment.

7 comments

  1. 's gravatar 19th October 2008

    david

    according to your documentation, fonts are placed in image_replacement/fonts directory

    so you should change in image_replacement.py from trunk:

    fontpath = path.join(settings.MEDIAROOT, ‘image_replacement’, ‘images’)

    to:

    fontpath = path.join(settings.MEDIAROOT, ‘image_replacement’, ‘fonts’)

    by the way r5 of this file seems not affected.

    Thanks a lot for this django filter, it’s definitely a better approach than dango-rendertext

    Have you ever tried cairo image generation as Andrew Godwin did ? django and cairo rendering pretty titles

    Perhaps bold and italic font limitation of PIL is not part of cairo features ;-)

  2. 's gravatar 25th October 2008

    Ludwig Pettersson

    Whops, fixed that david.

    I haven’t tried that actually, but I’ll look into it!

  3. 's gravatar 28th January 2009

    KR

    hello and thanks for that. curious if you’ve managed to use that with a inline list of unordered objects, like a nav-bar ?

    • 1
    • 2
    • 3
    and applying the font-replacement to each ?

  4. 's gravatar 3rd March 2009

    Antti Kaihola

    Alternative implementations of similar ideas:

  5. 's gravatar 6th March 2009

    Antti Kaihola

    django-cairo-text is a re-usable app based on code from Andrew Godwin’s blog post mentioned above.

  6. 's gravatar 1st October 2009

    Nedirrilestef

    porno

  7. 's gravatar 21st October 2009

    CawsAvaismCab

    Hi there, my home page is http://www.google.com

Write comment

Optional

No HTML is allowed, please use Markdown for text formatting.

Emails are never published, but are used to spice up your comment through Gravatar.