from PIL import Image, ImageDraw, ImageFont
import md5
import os
import sys
def mailhide(value):
email_md5 = md5.new(value).hexdigest()
email_path = os.path.join(MEDIA_ROOT, EMAIL_THUMBNAILS).replace('\\', '/')
em_file_path = os.path.join(email_path, email_md5 + '.png').replace('\\', '/')
if not os.path.exists(email_path):
os.mkdir(email_path)
if not os.path.exists(em_file_path):
img = Image.new('RGBA',(1,1))
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
draw = ImageDraw.ImageDraw(img)
w,h = draw.textsize(value, font = font)
img = img.resize((w,h))
draw = ImageDraw.ImageDraw(img)
draw.text((0,0),value, font = font, fill = FONT_COLOR)
img.save(em_file_path)
result = '<img src = "%s%s/%s.png"/>'%(MEDIA_URL, EMAIL_THUMBNAILS,email_md5)
return mark_safe(result)
Comments
I have updated this snippet slightly to allow the ability to configure each parameter per filter call and also adjust the letter spacing. The resulting image is also auto-cropped:
to be used as so:
#
Also, if you change
into
it anti-aliases non-black fonts correctly. You can also remove
and replace it with
#
Sorry one more change, if you use the hex_to_rgb snippet from http://stackoverflow.com/questions/214359/converting-hex-to-rgb-and-vice-versa , the new image line should actually read
#