共計(jì) 558 個字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
在 Python 中,可以使用 base64
庫將 base64
數(shù)據(jù)轉(zhuǎn)換為圖片。以下是將 base64
數(shù)據(jù)寫成圖片的示例代碼:
import base64
import io
from PIL import Image
def write_base64_image(base64_data, file_path):
# 解碼 base64 數(shù)據(jù)
image_data = base64.b64decode(base64_data)
# 創(chuàng)建 Image 對象
image = Image.open(io.BytesIO(image_data))
# 保存圖片
image.save(file_path)
# 示例調(diào)用
base64_data = "base64 數(shù)據(jù) "
file_path = " 圖片保存路徑 "
write_base64_image(base64_data, file_path)
在示例代碼中,write_base64_image
函數(shù)接受兩個參數(shù),分別是 base64
數(shù)據(jù)和要保存的文件路徑。首先,使用 base64.b64decode
函數(shù)將 base64
數(shù)據(jù)解碼為原始的圖片數(shù)據(jù)。然后,使用 io.BytesIO
將圖片數(shù)據(jù)轉(zhuǎn)換為 BytesIO
對象,并傳遞給 Image.open
函數(shù)創(chuàng)建 Image
對象。最后,使用 Image.save
方法將圖片保存為指定的文件路徑。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完