Created
June 29, 2013 10:35
-
-
Save cocuh/5890682 to your computer and use it in GitHub Desktop.
data uri schemeをワンコマンドで生成するなにか
Usage: dataurlcheme filename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os.path | |
import mimetypes | |
import binascii | |
def encode(filename, data): | |
mime = mimetypes.guess_type(filename)[0] | |
encoded = binascii.b2a_base64(data) | |
return "data:{mime};base64,{enc}".format(mime=mime,enc=encoded) | |
def usage(): | |
print """Usage: {cmd} file\n""".format(cmd=os.path.basename(__file__)) | |
if __name__ == '__main__': | |
args = sys.argv | |
if len(args) != 2: | |
usage() | |
raise TypeError("argument error") | |
filename = args[1] | |
data = open(filename).read() | |
sys.stdout.write(encode(filename, data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment