import base64 image = open('deer.gif', 'rb') image_read = image.read() image_64_encode = base64.encodestring(image_read) image_64_decode = base64.decodestring(image_64_encode) image_result = open('deer_decode.gif', 'wb') # create a writable image and write the decoding result image_result.write(image_64_decode)
Here is what the above code is Doing:
1. Open the image and read the content.
2. Encode the image with base64.
3. Decode the image with base64.
4. Create a writable image and write the decoding result.