-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.py
26 lines (21 loc) · 957 Bytes
/
decoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
### This module decodes the dataURL posted on the webpage into an
### image file, decoding it from base64.
import base64
def decode(dataURL, mode:int, num=0):
if mode==1:
with open("data_conv/image_decoded.png", "wb") as save:
save.write(base64.b64decode(dataURL))
if mode==2:
with open(f"data_conv/image_decoded{num}.png", "wb") as save:
save.write(base64.b64decode(dataURL))
else:
print('\nDECODE:\nUnhandled exception- bad "mode" input\n\n')
###Lower code used if text is manually saved to file
# img_data = ''
# with open('data.txt', 'r') as data: ### Gets base64 string from data.txt in directory
# img_data = data.readline()
# with open("imageToSave.png", "wb") as save:
# save.write(base64.b64decode(img_data)) ### decodes and saves into imageToSave.png
### If you want to manually input b64 (but copy+paste has limits)
# if __name__=="__main__":
# decode(input())