Python请求base64图片

标签 python base64 python-requests data-uri

我正在使用 requests 从远程 URL 获取图像。由于图像始终为 16x16,我想将它们转换为 base64,以便稍后嵌入它们以在 HTML img 标记中使用。

import requests
import base64
response = requests.get(url).content
print(response)
b = base64.b64encode(response)
src = "data:image/png;base64," + b

response 的输出是:

response = b'GIF89a\x80\x00\x80\x00\xc4\x1f\x00\xff\xff\xff\x00\x00\x00\xff\x00\x00\xff\x88\x88"""\xffff\...

HTML 部分是:

<img src="{{src}}"/>

但是图片不显示。

如何正确地对 响应 进行 base-64 编码?

最佳答案

我觉得是

import base64
import requests

response = requests.get(url)
uri = ("data:" + 
       response.headers['Content-Type'] + ";" +
       "base64," + base64.b64encode(response.content))

假设设置了content-type

关于Python请求base64图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30280495/

相关文章:

python - Linkedin 身份验证提供无效的 redirect_uri

python - 请求 SSL 连接超时

python - 枚举上的随机选择

python - 设置隐藏 ModelForm 字段 Django 的默认值

javascript - 如何下载由 JavaScript 生成的文件,避免自动打开 Chome?

iphone - NSURLConnection 与 base64 的兼容性

python 请求 : how to define a OR parameter

python - 具有多个基类的类中的继承属性问题

python sqlalchemy + postgresql程序卡住

ios - 如何将 base 64 图像数据字符串提供给 UIWebView?