在Python中的图像上大纲文本 [英] Outline text on image in Python

查看:56
本文介绍了在Python中的图像上大纲文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用PIL图片

我正在尝试在图像上绘制文本.我希望此文字像大多数模因一样具有黑色轮廓.我试图通过在前面的字母后面绘制较大字体的阴影字母来实现此目的.我已经相应地调整了阴影的x和y位置.阴影虽然略微掉了.前面的字母应恰好在阴影字母的中间,但事实并非如此.问号当然不是水平居中,所有字母垂直都太低.轮廓也不太好.


下面是产生上面图像的最小可复制示例.

产生上面图像的代码

从PIL导入

 导入Image,ImageDraw,ImageFontcaption =为什么Y和我看起来很奇怪?"x,y = 10,400字体= ImageFont.truetype(font ='./impact.ttf',size = 60)img = Image.open('./example-img.jpg')d = ImageDraw.Draw(img)shadowColor =(0,0,0)厚度= 4d.text((x-厚度,y-厚度),标题,字体=字体,填充=阴影颜色,厚=厚度)d.text((x +厚度,y-厚度),标题,字体=字体,填充=阴影颜色,厚=厚度)d.text((x-厚度,y +厚度),标题,字体=字体,填充=阴影颜色,厚=厚度)d.text((x +厚度,y +厚度),标题,字体=字体,填充=阴影颜色,厚=厚度)d.text((x,y),标题,间距= 4,填充=(255,255,255),字体=字体)#绘制文本img.save('example-output.jpg') 

解决方案

我不知道什么版本,但是关于

I have been using PIL Image

I am trying to draw text on an image. I want this text to have a black outline like most memes. I've attempted to do this by drawing a shadow letter of a bigger font behind the letter in front. I've adjusted the x and y postions of the shadow accordingly. The shadow is slightly off though. The letter in front should be exactly in the middle of the shadow letter, but this isn't the case. The question mark certainly isn't centered horizontally, and all the letters are too low vertically. The outline also just doesn't look good.


Below is a minimum reproducible example to produce the image above.

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 includes drawing the text 4 times in black behind the main text at positions slightly higher, slightly lower, slightly left, and slightly right, but these have also not been optimal as shown below

Code to produce the image above

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

解决方案

I don't know since what version, but about a year ago Pillow added text stroking. You probably need to update it if you haven't do so lately. Example usage with stroke_width of 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')

这篇关于在Python中的图像上大纲文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆