This commit is contained in:
Ross
2021-01-25 16:59:11 +00:00
parent c414bff991
commit 9d078f0131
3 changed files with 18 additions and 16 deletions
+15
View File
@@ -0,0 +1,15 @@
def image_as_base64(image_file):
"""
:param `image_file` for the complete path of image.
:param `format` is format for image, eg: `png` or `jpg`.
"""
# if not os.path.isfile(image_file):
# return None
encoded_string = ""
# with open(image_file, 'rb') as img_f:
# encoded_string = base64.b64encode(img_f.read())
encoded_string = base64.b64encode(image_file.file.read())
mimetype, enc = mimetypes.guess_type(image_file.path)
return "data:image/{};base64,{}".format(mimetype, encoded_string.decode("utf-8"))