Convert base64 to Image in Python

后端 未结 4 1881
走了就别回头了
走了就别回头了 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:05

    Your image file(jpeg/png) is encoded to base64 and encoded base64 string is stored in your mongo db. First decode the base64 string

    import base64
    image_binary=base64.decodestring(recovered_string_from_mongo_db)
    

    Now image_binary contains your image binary, write this binary to file

    with open('image.extension','wb') as f:
        f.write(image_binary)
    

    Where extension is your image file extension.

提交回复
热议问题