Skip to content

Instantly share code, notes, and snippets.

@mikeri
Created May 6, 2022 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeri/f12f3f70fe406f5e5b3af5d63d21c3a8 to your computer and use it in GitHub Desktop.
Save mikeri/f12f3f70fe406f5e5b3af5d63d21c3a8 to your computer and use it in GitHub Desktop.
CSS image/file embedder (generates data uri)
import base64
import argparse
import subprocess
parser = argparse.ArgumentParser()
parser.add_argument("file")
args = parser.parse_args()
mime_args = ("mimetype", "-b", args.file)
mime_type = subprocess.run(mime_args, capture_output=True).stdout.decode().strip()
with open(args.file, "rb") as fobj:
binary_data = fobj.read()
base64_data = base64.b64encode(binary_data).decode()
print(f'url("data:{mime_type};base64,{base64_data}")')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment