繁体   English   中英

尝试将 base64 字符串转换为 Python 中的图像时出错

[英]Error when trying to convert base64 string to image in Python

我正在尝试创建一个 Python 仪表板,用户在其中上传图像,然后分析图像。 上传的图像作为 base64 字符串接收,需要将其转换为图像。 我努力了

decoded = BytesIO(base64.b64decode(base64_string))
image = Image.open(decoded)

但我收到了这个错误:

无法识别图像文件 <_io.BytesIO object at 0x00000268954E9888>

Image需要一个类似 object 的文件,由open返回的那种东西。 最简单的方法是使用with语句:

decoded = base64.b64decode(base64)
with BytesIO(decoded) as fh:
    image = Image.open(fh)
    # do stuff with image here: when the with block ends
    # it's very likely the image will no longer be usable

看看这是否更适合你。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM