Created
May 6, 2022 08:01
-
-
Save mikeri/f12f3f70fe406f5e5b3af5d63d21c3a8 to your computer and use it in GitHub Desktop.
CSS image/file embedder (generates data uri)
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 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