我需要Discord.py的帮助。我一直在用python创建一个机器人,所以我想用这个机器人制作画布配置文件。问题是,我没有在Google中找到任何有关它的东西,只有node.js。我不想改写我的机器人,我想制作一个配置文件卡,例如:juniperbot,mee6。请帮帮我!
[我既不知道jupiterbot
也不知道mee6
,但是如果canvas
在Image manipulation with Canvas的文档中表示discord.js
,那么它仅用于生成图像,而仅将send()
用作普通文件.png
或[ C0]。
Python通常使用模块.jpg
生成或修改图像。 pillow,Image,ImageDraw
ImageFont
结果:
from discord.ext import commands
from discord import File
from PIL import Image, ImageDraw, ImageFont
import io
TOKEN = 'MY-TOKEN'
bot = commands.Bot(command_prefix='!')
@bot.command(name='canvas')
async def canvas(ctx, text=None):
IMAGE_WIDTH = 600
IMAGE_HEIGHT = 300
# create empty image 600x300
image = Image.new('RGB', (IMAGE_WIDTH, IMAGE_HEIGHT)) # RGB, RGBA (with alpha), L (grayscale), 1 (black & white)
# or load existing image
#image = Image.open('/home/furas/images/lenna.png')
# create object for drawing
draw = ImageDraw.Draw(image)
# draw red rectangle with green outline from point (50,50) to point (550,250) #(600-50, 300-50)
draw.rectangle([50, 50, IMAGE_WIDTH-50, IMAGE_HEIGHT-50], fill=(255,0,0), outline=(0,255,0))
# draw text in center
text = f'Hello {ctx.author.name}'
font = ImageFont.truetype('Arial.ttf', 30)
text_width, text_height = draw.textsize(text, font=font)
x = (IMAGE_WIDTH - text_width)//2
y = (IMAGE_HEIGHT - text_height)//2
draw.text( (x, y), text, fill=(0,0,255), font=font)
# create buffer
buffer = io.BytesIO()
# save PNG in buffer
image.save(buffer, format='PNG')
# move to beginning of buffer so `send()` it will read from beginning
buffer.seek(0)
# send image
await ctx.send(file=File(buffer, 'myimage.png'))
if __name__ == '__main__':
bot.run(TOKEN)