[英]Error when trying to convert base64 string to image in Python
I am trying create a Python dashboard in which a user uploads an image and then the image is analyzed.我正在尝试创建一个 Python 仪表板,用户在其中上传图像,然后分析图像。 The uploaded image is received as a base64 string and it needs to be converted to an image.上传的图像作为 base64 字符串接收,需要将其转换为图像。 I have tried我努力了
decoded = BytesIO(base64.b64decode(base64_string))
image = Image.open(decoded)
but I received this error:但我收到了这个错误:
cannot identify image file <_io.BytesIO object at 0x00000268954E9888>无法识别图像文件 <_io.BytesIO object at 0x00000268954E9888>
Image
needs a file-like object, the sort of thing returned by an open
. Image
需要一个类似 object 的文件,由open
返回的那种东西。 The easiest way to do this is using a with
statement:最简单的方法是使用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
See if that works better for you.看看这是否更适合你。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.