Skip to content

Instantly share code, notes, and snippets.

@franklinjavier
Last active December 15, 2015 20:49
Show Gist options
  • Save franklinjavier/5321154 to your computer and use it in GitHub Desktop.
Save franklinjavier/5321154 to your computer and use it in GitHub Desktop.
Convert image to base64 directly in to json file
#!/usr/bin/python
# -*- coding: utf-8
# Franklin Javier - Apr 10 2013
'''
Como usar
# chmod +x convert-base64.py
$ ./convert-base64.py . png
'''
import os
import sys
def convert(path, ext):
for root, subFolders, files in os.walk(path):
for file in files:
if file.endswith('.' + ext):
fileName = file.split('.' + ext)[0]
newFile = open(fileName + '.js', 'w')
i = open(file, 'rb').read()
newFile.write('Callback.result.hair({ \"hash\": \"data:image/png;base64,' + i.encode('base64').replace('\n', '') + '\" })')
newFile.close()
def main():
if len(sys.argv) < 2:
print (u'Faltou path e extensao do arquivo')
print (u'Ex: convert-base64.py . png')
return
convert(sys.argv[1], sys.argv[2])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment