base 64 encoding and decoding

import base64
encoded=base64.b64encode(bytes("this is a test",'utf-8'))
decoded=base64.b64decode(encoded).decode()

With an Image

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

Web Page Example

This is how to embed an image into your Web-Page.

import base64

with open('output.png','rb') as img:
    img_data=img.read()

#img_data starts off as being bytes
img_str=base64.encodebytes(img_data).decode('utf-8').replace('\n','')

# Generate a Web Page with the Embedded Image in it

with open("h.html","wt") as tml:
    tml.write("""
    <html>
    <head>Test</head>
    <page>""")
    tml.write("<img src=\"data:image/png;base64,{}\">".format(img_str))
    tml.write("</page></html>")
    tml.close()