1import base64
2
3file = 'deer.jpg'
4image = open(file, 'rb')
5image_read = image.read()
6image_64_encode = base64.encodebytes(image_read) #encodestring also works aswell as decodestring
7
8print('This is the image in base64: ' + str(image_64_encode))
9
10image_64_decode = base64.decodebytes(image_64_encode)
11image_result = open('deer_decode.jpg', 'wb') # create a writable image and write the decoding result
12image_result.write(image_64_decode)
13
14
1import base64
2import requests
3
4
5def get_as_base64(url):
6
7 return base64.b64encode(requests.get(url).content)
8