[Image-SIG] How to write a text to an image
Terry Carroll
carroll at tjc.com
Wed Jan 24 07:10:32 CET 2007
On Mon, 22 Jan 2007 export at hope.cz wrote:
> Is it possible to write a text , by using PIL, to a bottom corner of an
> image? Is there any doc/tutorial for that?
I use this routine (in a little app I wrote to extract the date a photo
was taken from its EXIF data, and add it to the bottom corner)
def Imprint(im, inputtext, font=None, color=None, opacity=.6, margin=(30,30)):
"""
imprints a PIL image with the indicated text in lower-right corner
"""
if im.mode != "RGBA":
im = im.convert("RGBA")
textlayer = Image.new("RGBA", im.size, (0,0,0,0))
textdraw = ImageDraw.Draw(textlayer)
textsize = textdraw.textsize(inputtext, font=font)
textpos = [im.size[i]-textsize[i]-margin[i] for i in [0,1]]
textdraw.text(textpos, inputtext, font=font, fill=color)
if opacity != 1:
textlayer = ReduceOpacity(textlayer,opacity)
return Image.composite(textlayer, im, textlayer)
The ReduceOpacity method is the one from this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
Which inspired my app in the first place
More information about the Image-SIG
mailing list