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
| # -*-coding: iso-8859-15 -*-
import Image, ImageDraw
def draw_text(im, position, texte, *args, **kwargs):
texte = texte.replace('\r\n', '\n')
texte = texte.replace('\r', '\n')
lignes = texte.split('\n')
x, y = position
try:
font = kwargs['font']
except KeyError:
font = None
for ligne in lignes:
im.text((x,y), ligne,*args, **kwargs)
if font:
y += im.textsize(ligne, font=font)[1]
else:
y += im.textsize(ligne)[1]
if __name__ == "__main__":
im = Image.new('RGB', (400, 400), color=(255, 255, 255))
im_draw = ImageDraw.Draw(im)
texte = "ceci est\nun texte\r\nmultiligne...."
draw_text(im_draw, (10,10), texte, fill=(255,0,0))
im.show() |
Partager