2018-12-08 01:55:28 +00:00
|
|
|
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:
|
2018-12-25 20:31:44 +00:00
|
|
|
namesonly = [f for f in os.listdir(folder) if f.endswith('.png') or f.endswith('.ico')]
|
2018-12-08 01:55:28 +00:00
|
|
|
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)
|
2018-12-25 20:31:44 +00:00
|
|
|
outfile.write('\n{} = {}\n\n'.format(file[:file.index(".")], encoded))
|
2018-12-08 01:55:28 +00:00
|
|
|
sg.OneLineProgressMeter('Base64 Encoding', i+1, len(namesonly),key='_METER_')
|
|
|
|
|
|
|
|
outfile.close()
|
2018-12-25 20:31:44 +00:00
|
|
|
sg.Popup('Completed!', 'Encoded %s files'%(i+1))
|
2018-12-08 01:55:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|