discord.py image“需要类似字节的对象,而不是'int'”

2024-10-01 04:56:57 发布

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

我试图在discord.py中创建一个图像操作命令,我希望它能够获取用户发送的图像并将其粘贴到另一个图像中,但每次都会导致discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: a bytes-like object is required, not 'int'"

这是我的密码:

@client.command()
async def procureajuda(ctx):
    pessoa = (ctx.message.attachments[0])
    hm = (pessoa)
    hm = (await hm.save(fp = 'ha.png', seek_begin = False, use_cached = False))
    h = BytesIO (hm)
    hm = Image.open (h)
    template = Image.open ("E:/Projetos/Copper/templates/procure ajuda.png")
    hm = hm.resize ((360,201))
    template.paste (hm, (0,0))
    template.save("pro.png")
    await ctx.send(file = discord.File("pro.png"))

我试着去掉BytesIO部分(因为我不知道还能做什么),结果出现了一个不同的错误,比如'int' object has no attribute 'read'

编辑:对不起,忘了回溯

Ignoring exception in command procureajuda:
Traceback (most recent call last):
  File "E:\programas 2\python 3.9\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "E:\Projetos\Copper\copper.py", line 78, in procureajuda
    h = BytesIO (hm)
TypeError: a bytes-like object is required, not 'int'

Tags: inpy图像objectpngtemplateawaitext
1条回答
网友
1楼 · 发布于 2024-10-01 04:56:57

discord发送支持io.BuffereDiabase作为参数。 您可以使用io.BytesIO尝试以下操作:

@client.command()
async def procureajuda(ctx):
   pessoa = (ctx.message.attachments[0])
   hm = (pessoa)
   hm = (await hm.save(fp = 'ha.png', seek_begin = False, use_cached = False))
   h = BytesIO (hm)
   hm = Image.open (h)
   template = Image.open ("E:/Projetos/Copper/templates/procure ajuda.png")
   hm = hm.resize ((360,201))
   template.paste (hm, (0,0))
   arr = io.BytesIO()
   template.save(arr,"pro.png")
   arr.seek(0)
   await ctx.send(file = discord.File(arr))

相关问题 更多 >