I'd like to be able to output an HTML file which includes an image (within the file itself). By Googling, I've come across a couple of ways to do this:
javascript:imageData
<IMG SRC="data:image/gif;base64,[...]">
<object ... >
tag (although this uses a data URI, so may inherit the same limitations)But I don't know which is better supported by browsers, or if there are other alternatives.
Can anyone with some practical experience of this offer me advice? Thanks.
HTML <img> data-* Attribute. A data-* attribute on an <img> tag attaches additional data to the image. To create a custom attribute, replace * with a lowercase string, such as data-id , data-status , or data-location .
To get this to work, once your bytes are loaded into an Image class, Create a BytesIO object and then use this to save the image with the format to the BytesIO, i.e img. save(bt_ob, format="png"). This code will now work in the html in the "data:image/png;base64,<>" format.
Base64 encoding is a way to encode binary data in ASCII text. It's primarily used to store or transfer images, audio files, and other media online. It is also often used when there are limitations on the characters that can be used in a filename for various reasons.
Image Encoding Overview. An encoder writes image data to a stream. Encoders can compress, encrypt, and alter the image pixels in a number of ways prior to writing them to the stream.
In regards to browser support, from Wikipedia:
Data URIs are currently supported by the following web browsers:
IE does not handle the <object>
tag correctly, see here for more details. Briefly, you can't trust IE to display images from it.
The javascript:imageData
is also not well-supported, since it is used in a lot of cross site scripting attacks.
There are many discussions of this on the web, and they all come to the conclusion that there is no good universal way to embed images. If you only need to support a subset of browsers, the data uri may work, or a combination of data uri and javascript.
imageData is more flexible, for instance some ie's restricts data uris to 32k.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With