将 PIL 图像转换为字节,出现错误
Convert PIL Image to Bytes, getting Error
我找到的解决方案是:
from PIL import Image
import io
img = "1.jpg"
image = Image.open(img)
# ... other processing...
buf = io.BytesIO()
image.save(buf, format="JPEG")
buf.get_value()
但我收到错误消息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.BytesIO' object has no attribute 'get_value'
如果我改为尝试:
buf.seek(0)
它只输出0。
这是我找到的仅有的两个建议,它们对我不起作用。是我的版本问题吗?
我有 Python 3.7.3 和 PIL 6.1.0
尝试:
>>> buf.seek(0) # Return to beginning of buffer
>>> data = buf.read() # Read all bytes until EOF
错误说明了一切,BytesIO
对象没有名为 get_value
的属性。该属性是 getvalue()
而不是 get_value()
。有关更多信息,请参阅文档 https://docs.python.org/3/library/io.html#io.BytesIO
我找到的解决方案是:
from PIL import Image
import io
img = "1.jpg"
image = Image.open(img)
# ... other processing...
buf = io.BytesIO()
image.save(buf, format="JPEG")
buf.get_value()
但我收到错误消息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.BytesIO' object has no attribute 'get_value'
如果我改为尝试:
buf.seek(0)
它只输出0。
这是我找到的仅有的两个建议,它们对我不起作用。是我的版本问题吗? 我有 Python 3.7.3 和 PIL 6.1.0
尝试:
>>> buf.seek(0) # Return to beginning of buffer
>>> data = buf.read() # Read all bytes until EOF
错误说明了一切,BytesIO
对象没有名为 get_value
的属性。该属性是 getvalue()
而不是 get_value()
。有关更多信息,请参阅文档 https://docs.python.org/3/library/io.html#io.BytesIO