Render label as image.
(label, fontname="courB08.pil", imgformat="PNG",
fgcolor=(0,0,0), bgcolor=(255,255,255),
rotate_angle=0)
| 29 | manage_addImage(self, id, imgfile) |
| 30 | |
| 31 | def 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 |