Convert base64 to Image in Python

后端 未结 4 1872
走了就别回头了
走了就别回头了 2020-12-03 14:31

I have a mongoDB database and I recover base64 data which corresponds to my Image.

I don\'t know how to convert base64 data to an Image.

4条回答
  •  抹茶落季
    2020-12-03 15:15

    You can try this:

    import base64 
    png_recovered = base64.decodestring(png_b64text)
    

    'png_b64text' contains the text from your mongoDB image field.

    Then you just write "png_recovered" to a file:

    f = open("temp.png", "w")
    f.write(png_recovered)
    f.close()
    

    Just replace 'png' with the correct format.

提交回复
热议问题