简体   繁体   English

如何将文件 stream 转换为 Python 中的数据 URI?

[英]How do I convert a file stream to a data URI in Python?

I have a file stream of an image in Python:我在 Python 中有一个图像的文件 stream:

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

How do I convert this to a data URI ?如何将其转换为数据 URI

'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAU...'

Encode it in base64, then remove the newlines.将其编码为 base64,然后删除换行符。

>>> '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x04\x87...'.encode('base64').replace('\n', '')
'iVBORw0KGgoAAAANSUhEUgAABI....'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM