繁体   English   中英

discord.py 图像“需要类似字节的 object,而不是 'int'”

[英]discord.py image “a bytes-like object is required, not 'int'”

我正在尝试在 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'

discord 发送支持 io.BufferedIOBase 作为参数。 你可以使用 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))

暂无
暂无

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

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