我通過 ajax 呼叫將檔案上傳到服務器。這些檔案包含影像。我需要閱讀上傳的檔案并將它們轉換為python中的.png。我怎樣才能做到這一點?
存盤為 .txt 檔案的影像如下所示:“data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAm4 ... RK5CYII="
謝謝!FP
uj5u.com熱心網友回復:
從 iV... 開始的字串是您在 base 64 中轉換的 png 的實際位元組資料。將字串解碼并作為二進制檔案轉換為檔案,您將獲得您的影像
import base64
png_str = "iVBORw0 ... NS=="
png_bytes = base64.b64decode(png_str) # get your raw bytes
with open('test.png', 'bw') as fid: # write as binary file
fid.write(png_bytes)
如果有幫助,請投票。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/519946.html
