Skip to content

Instantly share code, notes, and snippets.

@shiweifu
Created December 8, 2014 04:25
Show Gist options
  • Save shiweifu/4033eecf99363a1adeee to your computer and use it in GitHub Desktop.
Save shiweifu/4033eecf99363a1adeee to your computer and use it in GitHub Desktop.
covert binary file to base64 and save
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#author: shiweifu <[email protected]>
import base64
import argparse
def to_base64(data):
return base64.b64encode(data).decode()
def each_files(fun, *files):
s = [fun(open(f, "rb").read()) for f in files]
return s
def main():
"""
usage: python3 tobase64.py -o dst_file_with_base64 [imgpath, imgpath, ....]
"""
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
parser.add_argument('-o')
args = parser.parse_args()
dst_file = args.o
s = each_binary_files(to_base64, *args.filenames)
open(dst_file, "w").write("\n".join(s))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment