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

时间:2020-08-25 20:08:05

标签: python discord discord.py

我看到很多机器人在欢迎事件上发送自定义图像,例如koya bot发送带有欢迎信息的欢迎图像,上面印有用户的信息。

像这样: enter image description here

我不是在问on_member_join事件或任何我在问如何使我的机器人执行此操作的问题,就像我希望该机器人在图像上打印hello world那样,我该怎么办? 谢谢!

2 个答案:

答案 0 :(得分:2)

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

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

以下是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.show(),您可以保存用out.save('your-file-path.png')编辑的图像,并用channel.send(file=discord.File('your-file-path.png'))发送。

答案 1 :(得分:0)

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

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