繁体   English   中英

Python 中图像上的轮廓文本

[英]Outline text on image in Python

我一直在使用 PIL Image

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

在此处输入图像描述


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

链接到字体

链接到原始图像

 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')


另一种方法包括在主文本后面稍高、稍低、稍左和稍右的位置用黑色绘制 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')

我不知道从什么版本开始,但大约一年前Pillow 添加了文字抚摸。 如果您最近没有这样做,您可能需要更新它。 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')

您可以使用使用PILmathlibplot文本Stroke效果。

例子:

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')

结果: 结果

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

链接了解更多详情

代码:

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')

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM