简体   繁体   中英

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

I am trying to make an image manipulation command in discord.py, I want it to get the image an user sends and paste it into another image, but every time it results in discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: a bytes-like object is required, not 'int'"

Here is my code:

@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"))

I tried getting rid of the BytesIO part (because I did not know what else to do) and it resulted in a different error, something like 'int' object has no attribute 'read' .

Edit: sorry, forgot the traceback.

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 send support io.BufferedIOBase as parameter. you can use io.BytesIO try this:

@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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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