Is there a way to outline text with a dark line in PIL?

后端 未结 5 1498
轮回少年
轮回少年 2020-12-17 15:04

I\'m using python/PIL to write text on a set of PNG images. I was able to get the font I wanted, but I\'d like to now outline the text in black.

5条回答
  •  轮回少年
    2020-12-17 15:27

    import Image, ImageFont, ImageDraw
    
    import win32api, os
    
    x, y = 10, 10
    
    fname1 = "c:/test.jpg"
    im = Image.open(fname1)
    pointsize = 30
    fillcolor = "red"
    shadowcolor = "yellow"
    
    text = "hi there"
    
    font = win32api.GetWindowsDirectory() + "\\Fonts\\ARIALBD.TTF"
    draw = ImageDraw.Draw(im)
    font = ImageFont.truetype(font, pointsize)
    
    # thin border
    draw.text((x-1, y), text, font=font, fill=shadowcolor)
    draw.text((x+1, y), text, font=font, fill=shadowcolor)
    draw.text((x, y-1), text, font=font, fill=shadowcolor)
    draw.text((x, y+1), text, font=font, fill=shadowcolor)
    
    # thicker border
    draw.text((x-1, y-1), text, font=font, fill=shadowcolor)
    draw.text((x+1, y-1), text, font=font, fill=shadowcolor)
    draw.text((x-1, y+1), text, font=font, fill=shadowcolor)
    draw.text((x+1, y+1), text, font=font, fill=shadowcolor)
    
    # now draw the text over it
    draw.text((x, y), text, font=font, fill=fillcolor)
    
    fname2 = "c:/test2.jpg"
    im.save(fname2)
    
    os.startfile(fname2)
    

提交回复
热议问题