Question

I have a file stream of an image in Python:

\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x04\x87...

How do I convert this to a data URI?

'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAU...'
Was it helpful?

Solution

Encode it in base64, then remove the newlines.

>>> '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x04\x87...'.encode('base64').replace('\n', '')
'iVBORw0KGgoAAAANSUhEUgAABI....'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top