【问题标题】:Outline text on image in PythonPython中图像上的轮廓文本
【发布时间】:2020-10-25 10:58:56
【问题描述】:

我一直在使用 PIL 图片

我正在尝试在图像上绘制文本。我希望这个文本像大多数模因一样具有黑色轮廓。我试图通过在前面的字母后面画一个更大字体的阴影字母来做到这一点。我已经相应地调整了阴影的 x 和 y 位置。不过阴影稍微偏了一点。前面的字母应该正好在阴影字母的中间,但事实并非如此。问号肯定不是水平居中的,所有的字母在垂直方向上都太低了。轮廓也不好看。


下面是生成上图的最小可重现示例。

Link to the font

Link to original image

from PIL import Image, ImageDraw, ImageFont
    
    caption = "Why is the text slightly off?"
    img = Image.open('./example-img.jpg')
    d = ImageDraw.Draw(img)
    x, y = 10, 400
    font = ImageFont.truetype(font='./impact.ttf', size=50)
    shadowFont = ImageFont.truetype(font='./impact.ttf', size=60)
    for idx in range(0, len(caption)):
        char = caption[idx]
        w, h = font.getsize(char)
        sw, sh = shadowFont.getsize(char)  # shadow width, shadow height
        sx = x - ((sw - w) / 2)  # Shadow x
        sy = y - ((sh - h) / 2)  # Shadow y
        # print(x,y,sx,sy,w,h,sw,sh)
        d.text((sx, sy), char, fill="black", font=shadowFont)  # Drawing the text
        d.text((x, y), char, fill=(255,255,255), font=font)  # Drawing the text
        x += w + 5
    
    img.save('example-output.jpg')

Another approach 包括在正文后面略高、略低、略左、略右的位置用黑色绘制文本 4 次,但这些也不是最佳的,如下所示

生成上图的代码

    from PIL import Image, ImageDraw, ImageFont
    
    caption = "Why does the Y and i look weird?"
    x, y = 10, 400
    font = ImageFont.truetype(font='./impact.ttf', size=60)
    img = Image.open('./example-img.jpg')
    d = ImageDraw.Draw(img)
    shadowColor = (0, 0, 0)
    thickness = 4
    d.text((x - thickness, y - thickness), caption, font=font, fill=shadowColor, thick=thickness)
    d.text((x + thickness, y - thickness), caption, font=font, fill=shadowColor, thick=thickness)
    d.text((x - thickness, y + thickness), caption, font=font, fill=shadowColor, thick=thickness)
    d.text((x + thickness, y + thickness), caption, font=font, fill=shadowColor, thick=thickness)
    d.text((x, y), caption, spacing=4, fill=(255, 255, 255), font=font)  # Drawing the text
    img.save('example-output.jpg')

【问题讨论】:

标签: python image image-processing python-imaging-library


【解决方案1】:

不知道从什么版本开始,但是关于a year agoPillow 加了文字抚摸。如果您最近没有这样做,您可能需要更新它。使用stroke_width 2 的示例:

from PIL import Image, ImageDraw, ImageFont

caption = 'I need to update my Pillow'
img = Image.open('./example-img.jpg')
d = ImageDraw.Draw(img)
font = ImageFont.truetype('impact.ttf', size=50)
d.text((10, 400), caption, fill='white', font=font,
       stroke_width=2, stroke_fill='black')
img.save('example-output.jpg')

【讨论】:

    【解决方案2】:

    您可以使用mathlibplot 文字Stroke 效果,它使用PIL

    例子:

    import matplotlib.pyplot as plt
    import matplotlib.patheffects as path_effects
    import matplotlib.image as mpimg
    
    fig = plt.figure(figsize=(7, 5))
    fig.figimage(mpimg.imread('seal.jpg'))
    text = fig.text(0.5, 0.1, 'This text stands out because of\n'
                              'its black border.', color='white',
                              ha='center', va='center', size=30)
    text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='black'),
                           path_effects.Normal()])
    plt.savefig('meme.png')
    

    结果:

    【讨论】:

    • 您能否发布一个示例,说明如何将其绘制到图像而不是绘图上?
    • @Sam 我加了一个例子
    【解决方案3】:

    正如@Abang 指出的,使用stroke_width 和stroke_fill。

    Link for more details

    代码:

    from PIL import Image, ImageDraw, ImageFont
    
    caption = 'Ans: stroke_width & stroke_fill'
    img = Image.open('./example-img.jpg')
    d = ImageDraw.Draw(img)
    font = ImageFont.truetype('impact.ttf', size=50)
    d.text((60, 400), caption, fill='white', font=font, spacing = 4, align = 'center',
           stroke_width=4, stroke_fill='black')
    img.save('example-output.jpg')
    

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 1970-01-01
      • 2012-11-15
      • 2017-11-25
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多