Skip to content

Instantly share code, notes, and snippets.

@BigglesZX
Created April 19, 2012 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigglesZX/2423985 to your computer and use it in GitHub Desktop.
Save BigglesZX/2423985 to your computer and use it in GitHub Desktop.
Easily output an email address as a PNG image in Django
import Image
import ImageFont, ImageDraw
from django.conf import settings
from django.http import HttpResponse
from os.path import join
def emailshield(request):
address = 'info@example.com'
# change the path below to a TTF file you want to use
font = ImageFont.truetype(join(settings.PROJECT_ROOT, 'static', 'Vera.ttf'), 14)
size = font.getsize(address)
im = Image.new('RGBA', size, (0, 0, 0, 0))
draw = ImageDraw.Draw(im)
draw.text((0, 0), address, font=font, fill='black')
response = HttpResponse(mimetype='image/png')
im.save(response, "PNG")
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment