May 2020
Intermediate to advanced
404 pages
10h 52m
English
We might sometimes get images in the form of base64 encoded strings if the user makes an image POST request with a suitable setting. We can create a function to handle that:
import reimport base64def stringToImage(img): imgstr = re.search(r'base64,(.*)', str(img)).group(1) with open('image.png', 'wb') as output: output.write(base64.b64decode(imgstr))
We use the re module for regex to determine whether the data passed is in the form of a base64 string. The base64 module is needed to decode the string and then the file is saved as image.png.
Read now
Unlock full access