简体   繁体   中英

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

I am currently doing this for bytestring conversion but I need to convert to string.

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')

Here is my solution with 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') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM