![](/img/trans.png)
[英]Getting TypeError when trying to convert in-memory image to base64 string?
[英]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.