如何使我的机器人在discord.py中发送已编辑的图像?

2024-09-28 05:20:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在那里看到很多机器人,在欢迎活动上发送自定义图像,比如koya机器人发送一张印有用户信息的欢迎图像

像这样: enter image description here

我不是在问关于会员加入活动或任何事情,我是在问如何让我的机器人做到这一点,比如我想让机器人在图像上打印hello world,我该怎么做,我需要什么? 谢谢


Tags: 用户图像信息helloworld机器人事情会员
2条回答

查看ImageMagick的绘图-将形状或文本添加到图像https://imagemagick.org/Usage/draw/#text

也许您可以使用Wand,这是一种基于ctypes的Python简单ImageMagick绑定,下面是关于draw text https://docs.wand-py.org/en/0.6.2/guide/draw.html#texts的文档

基本上,您只需要一个用于编辑图像的库。对于Python,您可以使用最著名的pillow

然后,您只需通过discord.py检索用户名和其他要在图像上显示的信息,然后您就可以使用pillow的方法将文本放在背景图像上或您希望储存在机器人文件中的任何内容

以下是来自pillow's doc的示例:

from PIL import Image, ImageDraw, ImageFont
# get an image
base = Image.open("Pillow/Tests/images/hopper.png").convert("RGBA")

# make a blank image for the text, initialized to transparent text color
txt = Image.new("RGBA", base.size, (255,255,255,0))

# get a font
fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
# get a drawing context
d = ImageDraw.Draw(txt)

# draw text, half opacity
d.text((10,10), "Hello", font=fnt, fill=(255,255,255,128))
# draw text, full opacity
d.text((10,60), "World", font=fnt, fill=(255,255,255,255))

out = Image.alpha_composite(base, txt)

out.show()

您可以保存用out.save('your-file-path.png')编辑的图像,并用channel.send(file=discord.File('your-file-path.png'))发送,而不是out.show()

相关问题 更多 >

    热门问题