Login

txt2img tag to show on the web text as images

Author:
br0th3r
Posted:
April 29, 2012
Language:
Python
Version:
1.4
Tags:
tag text image templatetag hide convert
Score:
4 (after 4 ratings)

txt2img tag shows on the web text as images, helping to avoid get indexed email address and some other information you don't want to be on search engines.

Usage: {{worker.email|txt2img:18|safe}}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from django.conf import settings
from django.template import Library

import Image, ImageDraw, ImageFont
from os import path
import hashlib

register = Library()

@register.filter
def txt2img(text,FontSize=14,bg="#ffffff",fg="#000000",font="FreeMono.ttf"):
    '''
    txt2img tag shows on the web text as images, helping to avoid get
    indexed email address and     some other information you don't want
    to be on search engines. Fonts should reside in the same folder of txt2img.
    
    Usage:
    {{worker.email|txt2img:18|safe}}
    '''
    font_dir = settings.MEDIA_ROOT+"/txt2img/"   # Set the directory to store the images
    img_name_temp = text+"-"+bg.strip("#")+"-"+fg.strip("#")+"-"+str(FontSize) # Remove hashes
    img_name="%s.jpg" % (hashlib.md5(img_name_temp).hexdigest())
    if path.exists(font_dir+img_name): # Make sure img doesn't exist already
        pass
    else:   
        font_size = FontSize
        fnt = ImageFont.truetype(font_dir+font, font_size)
        w, h= fnt.getsize(text)
        img = Image.new('RGBA', (w, h), bg)
        draw = ImageDraw.Draw(img)
        draw.fontmode = "0" 
        draw.text((0,0), text, font=fnt, fill=fg)
        img.save(font_dir+img_name,"JPEG",quality=100)  
    imgtag = '<img src="'+settings.MEDIA_URL+'txt2img/'+img_name+'" alt="'+text+'" />'
    return imgtag

More like this

  1. django image bundle template tag library by amitu 7 years, 11 months ago
  2. Twitter template tags and filters by moxypark 4 years, 9 months ago
  3. Boolean Image Flag TemplateTag by MichaelAnckaert 6 years, 5 months ago
  4. Git media cache busting tag by adamlofts 5 years, 6 months ago
  5. Email Munger by cootetom 6 years, 4 months ago

Comments

Please login first before commenting.