Skip to content

Instantly share code, notes, and snippets.

@reustonium
Last active August 29, 2015 14:06
Show Gist options
  • Save reustonium/c7d2499d9a092f75032a to your computer and use it in GitHub Desktop.
Save reustonium/c7d2499d9a092f75032a to your computer and use it in GitHub Desktop.
png in <img>
import base64
with open("test.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
img_tag = '<img src="data:image/png;base64,{0}">'.format(encoded_string)
print(img_tag)
@thequbit
Copy link

import base64

with open("yourfile.ext", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())

Via:

http://stackoverflow.com/questions/3715493/encoding-an-image-file-with-base64

@reustonium
Copy link
Author

I'm clearly missing something here:

Output:
<img src="data:image/png;base64,b'iVBORw0KGgoAAAANSUhEUgAAA2AAAAJACAYAAADrSQUmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X90n3V99/FXmpRfbSpNEwuUMaToBDl474yVWqcoFCswkChVUae0SHSe2WGV3hNu9EYmnsF2ZMfbDSIiw8nmj9raWZQjFEEPBQV/MM9QpFiwlrZZ2tqSwtqmuf/wUClE/NW8r9A+Hn811/fKlXeST5s+c32v69syNDQ0FAAAAEbcmKYHAAAA2FsIMAAAgCICDAAAoIgAAwAAKCLAAAAAiggwAACAIgIMAACgiAADAAAoIsAAAACKCDAAAIAiAgwAAKCIAAMAACgiwAAAAIoIMAAAgCICDAAAoIgAAwAAKCLAAAAAiggwAACAIgIMAACgiAADAAAoIsAAAACKCDAAAIAiAgwAAKCIAAMAACgiwAAAAIoIMAAAgCICDAAAoIgAAwAAKCLAAAAAiggwAACAIgIMAACgiAADAAAoIs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment