简体   繁体   English

使用 PIL 向图像添加文本

[英]Adding text to a image using PIL

So, I want the bot to send a welcome card with the user's profile picture & text saying " Welcome {user.name} ", but the text part isn't working.所以,我希望机器人发送一张带有用户个人资料图片和文字的欢迎卡,上面写着“欢迎 {user.name}”,但文字部分不起作用。 I have got no error in the console.我在控制台中没有错误。

Here's my code:这是我的代码:

from PIL import Image, ImageFilter, ImageFont, ImageDraw
from io import BytesIO

@client.event
async def on_member_join(member):

    wc = Image.open("wc.jpg")

    asset = member.avatar_url_as(size=128)
    data = BytesIO(await asset.read())
    pfp = Image.open(data)

    draw = ImageDraw.Draw(wc)
    font = ImageFont.truetype("Littlecandy.ttf", 24)
    
    pfp = pfp.resize((474,382))
    draw.text((549,284), f"{member.display_name}", (171, 5, 171), font=font)
    wc.paste(pfp, (727,209))
    wc.save("wcm.jpg")

    await client.get_channel(850634788633182218).send(file = discord.File('wcm.jpg'))

So, i have not got the answer but when i removed the RGB code that is this (171, 5, 171) than i tested it & it worked.所以,我没有得到答案,但是当我删除了这个(171, 5, 171)的 RGB 代码时,我对其进行了测试并且它有效。

Here's my changed code:这是我更改的代码:

from PIL import Image, ImageFilter, ImageFont, ImageDraw
from io import BytesIO

@client.comamnd()
async def on_member_join(member):

    wc2 = Image.open("wc2.jpg")

    asset = member.avatar_url_as(size=64)
    data = BytesIO(await asset.read())
    pfp = Image.open(data)

    draw = ImageDraw.Draw(wc2)
    font = ImageFont.truetype("BalsamiqSans-BoldItalic.ttf", 45)
    text = f"{member}" 

    pfp = pfp.resize((211,181))
    wc2.paste(pfp, (30,28))
    draw.text((26,235),text,font=font,fill='orange')
    wc2.save("wcm.jpg")

    await client.get_channel(850634788633182218).send(file = discord.File('wcm.jpg'))

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

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