Daniel Tam
Daniel Tam

Reputation: 926

How to manipulate an image on discord.py with Pillow?

I'm trying to create an image manipulation command for discord.py. However, this error keeps appearing whenever I run the command.

C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\PIL\Image.py:2884: RuntimeWarning: coroutine 'Asset.read' was never awaited
  fp = io.BytesIO(fp.read())
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Ignoring exception in on_command_error
Traceback (most recent call last):
  File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\PIL\Image.py", line 2882, in open
    fp.seek(0)
AttributeError: 'Asset' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\danie\Documents\Scripts\Bots\DiscordBot\skybot.py.py", line 802, in push
    im2 = Image.open(member.avatar_url)
  File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\PIL\Image.py", line 2884, in open
    fp = io.BytesIO(fp.read())
TypeError: a bytes-like object is required, not 'coroutine'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\danie\Documents\Scripts\Bots\DiscordBot\skybot.py.py", line 237, in on_command_error
    raise error
  File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: a bytes-like object is required, not 'coroutine'

This is my current code for the command. I dont' understand what a bytes are and I'm just really confused. Will someone help me out? Basically my push command is supposed to be a picture of the author's avatar pushing another person's avatar down a cliff. That's my idea.

@client.command()
async def push(ctx, member: discord.Member):
    author = ctx.author
    embed = discord.Embed(title="Push :D", description = None)
    im1 = Image.open(fp=r'C:\Users\danie\Documents\Scripts\Bots\DiscordBot\Img Commands\peakcommand.jpg')
    im2 = Image.open(member.avatar_url)
    im3 = Image.open(author.avatar_url)
    im1.paste(im2)
    im1.paste(im3)
    img1.save(r'C:\Users\danie\Documents\Scripts\Bots\DiscordBot\Img Commands\completedpeak.jpg')
    file = discord.File(r"C:\Users\danie\Documents\Scripts\Bots\DiscordBot\Img Commands", filename = "completedpeak.jpg")
    embed.set_image(url="attachment://completedpeak.jpg")
    await ctx.send(file=file, embed=embed)

Upvotes: 0

Views: 6858

Answers (1)

user14030743
user14030743

Reputation:

Your Code logic is fine I guess,The only area where the code might have gone a little out of track would be at the time where you were feeding the path in the sting from.:

here:

im1 = Image.open(fp='Users\danie\Documents\Scripts\Bots\DiscordBot\Img Commands\peakcommand.jpg')

There the string is not raw, which means that the characters after the backslash() will be collectively assumed to be an escape sequence character. so a simple solution that I would suggest you is to use (r) before your string.

Like this:

im1 = Image.open(fp=r'Users\danie\Documents\Scripts\Bots\DiscordBot\Img Commands\peakcommand.jpg')

Try this:

And if this doesnt work I have written a small program that uses PIL module to change the img of Tkinter logo, you can use it as a reference for writing proper syntax while importing an image.

from tkinter import * from PIL import ImageTk,Image

root= Tk()
img = ImageTk.PhotoImage(Image.open(r'C:\Users\Nexus\Desktop\IMG_20200531_092328-01.jpeg'))
root.iconphoto(False,img)


root.mainloop()

Upvotes: 1

Related Questions