簡體   English   中英

如何將 PIL 圖像轉換為字符串,然后將其轉換回 PIL 圖像? 無需將其保存到新文件即可完成轉換

[英]how to convert a PIL image to string and then convert it back to PIL image? The convsersion is to be done without saving it to a new file

我目前正在為字節字符串轉換執行此操作,但我需要轉換為字符串。

img=Image.fromarray(img)
output = io.BytesIO()
img.save(output, format="png")
image_as_string = output.getvalue()
img=Image.open(io.BytesIO(image_as_string))
img.save('strimg.png')

這是我使用 base64 的解決方案。

import base64

img = Image.open("test.png")
output = io.BytesIO()
img.save(output, format="png")
image_as_string = base64.b64encode(output.getvalue())

#encrypting/decrypting

img=Image.open(io.BytesIO(base64.b64decode(image_as_string)))
img.save('string.png') 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM