Login

hide emails with PIL - template filter

Author:
dekomote
Posted:
October 24, 2009
Language:
Python
Version:
1.1
Tags:
email mailhide
Score:
1 (after 1 ratings)

This template filter converts email text to image with the email text. It uses PIL and it makes the image as high and wide as the text.

This template filter is intended to be used as anti-spider protection.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)

More like this

  1. Dynamic image generator by pcholt 6 years, 4 months ago
  2. Unsharp Mask with PIL and PythonMagick by VidJa 6 years, 3 months ago
  3. Email Munger by cootetom 6 years, 3 months ago
  4. Simple "html email with images" sender by andres_torres_marroquin 4 years, 1 month ago
  5. Email obfuscation filter using ROT13 by worksology 5 years, 11 months ago

Comments

NickJones (on November 18, 2010):

Also, if you change

img = Image.new('RGBA', (1,1))

into

img = Image.new('RGBA', (1,1), (255,0,0,0))

it anti-aliases non-black fonts correctly. You can also remove

draw = ImageDraw.ImageDraw(img)
w,h = draw.textsize(value, font=font)

and replace it with

w,h = font.getsize(value)

#

Please login first before commenting.