首页 > 解决方案 > AttributeError:“图像”对象没有属性“getvalue”(PIL)

问题描述

我目前正在开发一个将两个图像与 PIL 粘贴在一起的程序,但是 PIL 很奇怪,所以我必须做一些额外的事情才能使用链接。无论如何,现在我不能使用 PIL 输出的内容,因为:AttributeError: 'Image' object has no attribute 'getvalue'

这是我的代码的重要部分:

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

谁能告诉我我缺少哪一行代码来解决这个问题?或者我做错了什么?

标签: pythonimageimage-processingpython-imaging-library

解决方案


图像对象确实没有getvalue方法,它是BytesIO实例。在这里你应该打电话buf.getvalue而不是first_image.getvalue.

buf = io.BytesIO()
first_image.save(buf, "png")
first_image = first_image.getvalue()

您的代码看起来有点像这样:https ://stackoverflow.com/a/33117447/7051394 ;但是如果你看一下那个sniper的最后三行,imgByteArr仍然是 a BytesIO,所以imgByteArr.getvalue()是有效的。


推荐阅读