python - 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()
谁能告诉我我缺少哪一行代码来解决这个问题?或者我做错了什么?
解决方案
图像对象确实没有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()
是有效的。
推荐阅读
- windows - 批处理脚本仅遍历具有特定随机名称的子文件夹
- python - 英特尔 MKL 错误:进入 DGELSD 时参数 6 不正确
- javascript - JS/CSS/Bootstrap - 从按钮组中选择按钮的样式
- excel - 具有多个条件的条件格式
- reactjs - 如何从 unpkg 下载包或添加依赖项 unpkg 包?
- assembly - 更改为更改寄存器文件的多周期实现,使其只有一个读取端口
- sequelize.js - 按特定值排序顺序
- r - 数据框中的粗体数字
- pine-script - 如何允许止损多头进场仅对下一个蜡烛有效,如果在 Pine-script 中未实现则过期?
- css - 响应式 CSS- 在交替行上交换元素位置