Python 属性错误:';图像';对象没有属性';getvalue';(PIL)
我目前正在开发一个程序,将两个图像与PIL粘贴在一起,但PIL很奇怪,所以我不得不做一些额外的工作,以便使用链接。无论如何,现在我不能使用PIL输出的内容,因为: AttributeError:“Image”对象没有属性“getvalue” 以下是我的代码的重要部分:Python 属性错误:';图像';对象没有属性';getvalue';(PIL),python,image,image-processing,python-imaging-library,Python,Image,Image Processing,Python Imaging Library,我目前正在开发一个程序,将两个图像与PIL粘贴在一起,但PIL很奇怪,所以我不得不做一些额外的工作,以便使用链接。无论如何,现在我不能使用PIL输出的内容,因为: AttributeError:“Image”对象没有属性“getvalue” 以下是我的代码的重要部分: async with aiohttp.ClientSession() as session: async with session.get(avurl) as second_image: image_byt
async with aiohttp.ClientSession() as session:
async with session.get(avurl) as second_image:
image_bytes = await second_image.read()
with Image.open(BytesIO(image_bytes)).convert("RGB") as first_image:
output_buffer = BytesIO()
first_image.save(output_buffer, "png")
output_buffer.seek(0)
async with aiohttp.ClientSession() as session:
async with session.get("https://i.imgur.com/dNS0WJO.png") as second_image:
image_bytes = await second_image.read()
with Image.open(BytesIO(image_bytes)) as second_image:
output_buffer = BytesIO()
second_image.save(output_buffer, "png")
output_buffer.seek(0)
first_image.paste(second_image, (0, 0))
buf = io.BytesIO()
first_image.save(buf, "png")
first_image = first_image.getvalue()
有人能告诉我我缺少哪行代码来修复这个问题吗?或者可能是我做错了什么?图像对象确实没有
getvalue
方法,而是BytesIO
实例。
这里您应该调用buf.getvalue
,而不是first\u image.getvalue
buf = io.BytesIO()
first_image.save(buf, "png")
first_image = first_image.getvalue()
您的代码看起来有点像这样:;但是,如果您查看该snipper的最后三行,
imgByteArr
仍然是一个BytesIO
,因此imgByteArr.getvalue()
是有效的。下面是一个更简单、可能更快的代码重新编写:
async with aiohttp.ClientSession() as session:
image1data = await session.get(avurl).read()
image2data = await session.get("https://i.imgur.com/dNS0WJO.png").read()
image1 = Image.open(BytesIO(image1data)).convert("RGB")
image2 = Image.open(BytesIO(image2data))
image1.paste(image2, (0, 0))
buf = BytesIO()
image1.save(buf, "png")
composite_image_data = buf.getvalue()
您将得到包含合成图像的PNG数据的
composite_image_data
。如果我先保存(buf,“PNG”)buf=buf.getvalue()file=discord.file(fp=buf,filename=“pfp.PNG”)我得到:UnicodeDecodeError:“utf-8”编解码器无法解码位置0处的字节0x89:无效的起始字节这看起来是另一个问题,最好再问另一个问题