我在发送不协调的图像时遇到了一些问题。我决定使用Pillow库来创建图像,并且我想发送由该库创建的图像,而不保存。我找到了可以将Image对象转换为二进制数据的方法,并放入fp参数。但它引发了编码错误。
代码:
image = Image.open("test.png")
image_binary = BytesIO()
image.save(image_binary, "PNG")
image_binary = image_binary.getvalue()
await ctx.send(file=discord.File(fp=image_binary))
错误:
Traceback (most recent call last):
File "D:ProjectsPythonphoenixvenvlibsite-packagesdiscordextcommandscore.py", line 79, in wrapped
ret = await coro(*args, **kwargs)
File "D:ProjectsPythonphoenixmoduleswelcome.py", line 25, in test_image
await ctx.send(file=discord.File(fp=image_binary))
File "D:ProjectsPythonphoenixvenvlibsite-packagesdiscordfile.py", line 68, in __init__
self.fp = open(fp, 'rb')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
image = Image.open("test.png")
with BytesIO() as image_binary:
image.save(image_binary, "PNG")
image_binary.seek(0)
await ctx.send(file=discord.File(fp=image_binary,filename="image.png"))