Add Text on Image using PIL

后端 未结 8 1251
后悔当初
后悔当初 2020-12-07 08:02

I have an application that loads an Image and when the user clicks it, a text area appears for this Image (using jquery), where user can write some text on the

8条回答
  •  感情败类
    2020-12-07 08:37

    First, you have to download a font type...for example: https://www.wfonts.com/font/microsoft-sans-serif.

    After that, use this code to draw the text:

    from PIL import Image
    from PIL import ImageFont
    from PIL import ImageDraw 
    img = Image.open("filename.jpg")
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype(r'filepath\..\sans-serif.ttf', 16)
    draw.text((0, 0),"Draw This Text",(0,0,0),font=font) # this will draw text with Blackcolor and 16 size
    
    img.save('sample-out.jpg')
    

提交回复
热议问题