MCPcopy Create free account
hub / github.com/ActiveState/code / txt2img

Function txt2img

recipes/Python/483756_Dynamic_Generatiimage/recipe-483756.py:31–51  ·  view source on GitHub ↗

Render label as image.

(label, fontname="courB08.pil", imgformat="PNG",
            fgcolor=(0,0,0), bgcolor=(255,255,255),
            rotate_angle=0)

Source from the content-addressed store, hash-verified

29 manage_addImage(self, id, imgfile)
30
31def txt2img(label, fontname="courB08.pil", imgformat="PNG",
32 fgcolor=(0,0,0), bgcolor=(255,255,255),
33 rotate_angle=0):
34 """Render label as image."""
35 font = ImageFont.load(os.path.join(fontpath,fontname))
36 imgOut = Image.new("RGBA", (20,49), bgcolor)
37
38 # calculate space needed to render text
39 draw = ImageDraw.Draw(imgOut)
40 sizex, sizey = draw.textsize(label, font=font)
41
42 imgOut = imgOut.resize((sizex,sizey))
43
44 # render label into image draw area
45 draw = ImageDraw.Draw(imgOut)
46 draw.text((0, 0), label, fill=fgcolor, font=font)
47
48 if rotate_angle:
49 imgOut = imgOut.rotate(rotate_angle)
50
51 return imgOut
52
53
54

Callers 2

zope_txt2imgFunction · 0.85
recipe-483756.pyFile · 0.85

Calls 7

loadMethod · 0.45
joinMethod · 0.45
newMethod · 0.45
DrawMethod · 0.45
resizeMethod · 0.45
textMethod · 0.45
rotateMethod · 0.45

Tested by

no test coverage detected