簡體   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