From 03a51c2e04e46e83bbdefcce37c023e523592f18 Mon Sep 17 00:00:00 2001 From: neingeist Date: Fri, 2 Dec 2016 16:40:41 +0100 Subject: [PATCH] b64-image: Convert an image to CSS/HTML base64 foo --- b64-image | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 b64-image diff --git a/b64-image b/b64-image new file mode 100755 index 0000000..4dd82e0 --- /dev/null +++ b/b64-image @@ -0,0 +1,22 @@ +#!/usr/bin/python3 +# Convert an image to CSS/HTML base64 foo + +from __future__ import division, print_function +import base64 +import magic +import sys + +if not hasattr(magic, "from_file"): + print("wrong magic module installed? try pip install python-magic") + sys.exit(1) + + +if len(sys.argv[1:]) == 0: + print("Usage: " + sys.argv[0] + " IMAGE.PNG ...") + sys.exit(1) + +for filename in sys.argv[1:]: + mimetype = magic.from_file(filename, mime=True) + with open(filename, "rb") as f: + encoded = base64.b64encode(f.read()) + print("url(data:{};base64,{})".format(mimetype, encoded))