forked from PySimpleGUI/PySimpleGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo_Base64_Image_Encoder.py
More file actions
40 lines (31 loc) · 1.26 KB
/
Demo_Base64_Image_Encoder.py
File metadata and controls
40 lines (31 loc) · 1.26 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import PySimpleGUI as sg
import os
import base64
'''
Base64 Encoder - encodes a folder of PNG files and creates a .py file with definitions
'''
OUTPUT_FILENAME = 'output.py'
def main():
# folder = r'C:\Python\PycharmProjects\GooeyGUI\Uno Cards'
folder=''
folder = sg.PopupGetFolder('Source folder for images\nImages will be encoded and results saved to %s'%OUTPUT_FILENAME,
title='Base64 Encoder',
default_path=folder, initial_folder=folder )
if folder is None or folder == '':
sg.PopupCancel('Cancelled - No valid folder entered')
return
try:
namesonly = [f for f in os.listdir(folder) if f.endswith('.png')]
except:
sg.PopupCancel('Cancelled - No valid folder entered')
return
outfile = open(os.path.join(folder, OUTPUT_FILENAME), 'w')
for i, file in enumerate(namesonly):
contents = open(os.path.join(folder, file), 'rb').read()
encoded = base64.b64encode(contents)
outfile.write(f'{file[:file.index(".")]} = {encoded}\n')
sg.OneLineProgressMeter('Base64 Encoding', i+1, len(namesonly),key='_METER_')
outfile.close()
sg.Popup('Completed!', 'Encoded %s files'% i)
if __name__ == '__main__':
main()