如何将图像直接下载到 bytesIO 变量? [英] How to download an image straight to a bytesIO variable?

查看:38
本文介绍了如何将图像直接下载到 bytesIO 变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像的现有 url,我想将图像直接下载到变量中(无需实际下载,也许可以从响应中获取?

I have an existing url of an image, I want to download the image straight to a variable (no need to actually download it, maybe get it from a response?

最终结果将是将图像下载到 BytesIO() 变量中".

The end result will be "download an image into a BytesIO() variable".

这样做的正确方法是什么?

What is the correct way to do so?

推荐答案

您可以使用请求:

import requests
from io import BytesIO

response = requests.get(url)
image_data = BytesIO(response.content)

注意这适用于 Python 3.X

Note this works in Python 3.X

这篇关于如何将图像直接下载到 bytesIO 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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