【问题标题】:Writing a text over an image that is on top of another image using PIL使用 PIL 在另一个图像之上的图像上写入文本
【发布时间】:2019-01-17 07:58:30
【问题描述】:
from PIL import Image, ImageDraw, ImageFont, ImageOps

命令:

    font1 = ImageFont.truetype('timesbd.ttf',17)

    backgtound = Image.open('plan.png')
    bar = Image.open("bar.png")
    write = ImageDraw.Draw(bar)
    write.text(xy=(73, 181), text="{} / 20".format(numbertotal), fill=(255, 255, 255), font=font1)
    background.paste(bar, (2, 173), bar)
    background.save('plan2.png')

我在一个名为“计划”的图像之上有一张名为“条形图”的图片,我正在尝试在图像“条形图”上写一个文本,但文本不在第二个图像的顶部,只有第一个一,谁能帮帮我? (x,y坐标正确)

【问题讨论】:

    标签: python python-imaging-library bots discord


    【解决方案1】:

    IIUC,并且您希望文本覆盖图像bar,问题是您在粘贴后编写文本。相反,您可以先在图像bar 上写下您的文字,然后将其粘贴到background

    在本例中,plan.png 是狗,bar.png 是猫。可以看到文字在bar之上,而不是plan之上:

    font1 = ImageFont.truetype('timesbd.ttf',17)
    
    background = Image.open('plan.png')
    bar = Image.open("bar.png")
    write = ImageDraw.Draw(bar)
    write.text(xy=(79, 181), text="my text", fill=(255, 255, 255), font=font1)
    background.paste(bar)
    background.save('plan2.png')
    

    【讨论】:

    • 我根据您写的内容编辑了问题,但即使如此,文字也没有留在第二张图片的顶部,为什么?
    猜你喜欢
    • 1970-01-01
    • 2013-11-02
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 2012-11-17
    相关资源
    最近更新 更多