-
-
Save wonderbeyond/15ab54d3a49c90bdbfd8afd232203e37 to your computer and use it in GitHub Desktop.
Generate Data URL from image file
This file contains hidden or 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 mimetypes | |
import base64 | |
def make_data_url(filename): | |
prefix = 'data:{0};base64,'.format(mimetypes.guess_type(filename)[0]) | |
with open(filename, 'rb') as f: | |
content = f.read() | |
data_url = prefix + base64.b64encode(content).decode() | |
return data_url | |
# make_data_url('/tmp/t.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment