Example of how to convert a png image to base64 using python:
Install pybase64 module
with pip
pip install pybase64
with conda
conda install -c conda-forge pybase64
Convert a png image to base64
import base64
with open("img.png", "rb") as img_file:
encoded_string = base64.b64encode(img_file.read())
print(encoded_string)
returns for example
b'iVBORw0KGgo...ozAAAAABJRU5ErkJggg=='
Add an image encoded in base64 with markdown
The above string image can be used with markdown:
<img src="data:image/png;base64, encoded_string" >
Example:
<img src="data:image/png;base64,iVBORw0K...YKRozAAAAABJRU5ErkJggg==" />
References
Links | Site |
---|---|
Encoding and Decoding Base64 Strings in Python | stackabuse.com |
Convert Image to Base64 string in Python | codespeedy |
Convert Image to Base64 string in Python | codespeedy |
Matplotlib graphic image to base64 | stackoverflow |
Converting matplotlib png to base64 for viewing in html template | stackoverflow |
conda-forge | anaconda.org |