Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Last active April 19, 2020 20:47
Show Gist options
  • Select an option

  • Save openroomxyz/d6be63bd5d1bc604a844b3869a4137a4 to your computer and use it in GitHub Desktop.

Select an option

Save openroomxyz/d6be63bd5d1bc604a844b3869a4137a4 to your computer and use it in GitHub Desktop.
Python : How to generate html with base64 encoded image?
import base64
text = """
<html><body>
<img src="data:image/png;base64,ae8f0c79-2893-4a82-8576-7478e18a5356" alt="Red dot" />
</body></html>
"""
def Image_To_HTML_64_EncodedImage(path_to_image, out_path):
with open(path_to_image, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
file = open(out_path,"w")
file.write(text.replace("ae8f0c79-2893-4a82-8576-7478e18a5356", encoded_string.decode('utf-8')))
file.close()
Image_To_HTML_64_EncodedImage("C:\\Users\\x\\Desktop\\x\\b.png","C:\\Users\\x\\Desktop\\x\\output.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment