display byte format jpeg in img

I have a byte format jpeg that I get from python that looks like this b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\...'. How can I display that inside an HTML img tag? Any help is appreciated. Thanks in advance.

>Solution :

Encode the bytes to base64, then use the native HTML b64 format for your image:

from base64 import b64encode

bytez = b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01'
b64_encoded = b64encode(bytez).decode()
>>> '/9j/4AAQSkZJRgABAQ=='

and then:

<img src="data:image/jpg;base64, /9j/4AAQSkZJRgABAQ==" alt="Your image" />

Leave a Reply