Welcome, guest | Sign In | My Account | Store | Cart

In the Web domain, sometimes it is necessary to dynamically create images containg just text. This recipe uses PIL to do just that, and it can be integrated to Zope/Plone through External Methods.

Python, 57 lines
 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import sys
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageChops
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO


# Fix this path to match your configuration
fontpath = "/usr/local/src/python/multimedia/Imaging-1.1.5/pilfonts"

def zope_txt2img(self, id, label, imgformat="PNG", **kw):
    """
    Copy this module to your Zope Instance Extensions directory,
    and after restarting Zope do create an External Method in ZMI
    following the example:

     Id - txt2img 
     Title - Convert text into image
     Module Name - txt2img.py
     Function Name - zope_txt2img

    """
    from OFS.Image import manage_addImage	
    imgfile = StringIO()
    img = txt2img(label, **kw)
    img.save(imgfile, imgformat)	
    manage_addImage(self, id, imgfile)

def txt2img(label, fontname="courB08.pil", imgformat="PNG",
            fgcolor=(0,0,0), bgcolor=(255,255,255),
            rotate_angle=0):
    """Render label as image."""
    font = ImageFont.load(os.path.join(fontpath,fontname))
    imgOut = Image.new("RGBA", (20,49), bgcolor)

    # calculate space needed to render text
    draw = ImageDraw.Draw(imgOut)
    sizex, sizey = draw.textsize(label, font=font)

    imgOut = imgOut.resize((sizex,sizey))

    # render label into image draw area
    draw = ImageDraw.Draw(imgOut)
    draw.text((0, 0), label, fill=fgcolor, font=font)

    if rotate_angle:
        imgOut = imgOut.rotate(rotate_angle)

    return imgOut



if __name__ == "__main__":
    img = txt2img("fulano@belex.com.br", "helvR10.pil")
    img.save('output.png')

Two frequent use cases are: 1)generate vertical or rotated labels, 2) generate an e-mail address as an image to avoid spam-scavengers. This recipe shows how to do it using PIL.

1 comment

Jakub Jankiewicz 17 years, 2 months ago  # | flag

Unused function argument. Function txt2img has 'imgformat' as argument but it never used it.