Python 圖片和 base64 轉換

將圖片檔案轉換為 base64 字串:
    
import base64

path = "C:\\Users\\ruyut\\Desktop\\r.png"
with open(path, "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read()).decode("utf-8")

print(encoded_string)
    

將 base64 字串轉換為圖片檔案:
    
import base64

base64_string = "base64字串"

with open("output.jpg", "wb") as image_file:
    image_file.write(base64.b64decode(base64_string))
    

留言