Python请求base64图像 [英] Python requests base64 image

查看:174
本文介绍了Python请求base64图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using requests to get the image from remote URL. Since the images will always be 16x16, I want to convert them to base64, so that I can embed them later to use in HTML img tag.

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

响应的输出为:

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}}"/>

但是没有显示图像.

如何正确对64位的响应代码进行base-64编码?

How can I properly base-64 encode the response?

推荐答案

我认为这只是

import base64
import requests

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

假设设置了 content-type .

这篇关于Python请求base64图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆