一、代码
from PIL import Image
import requests
from io import BytesIO
import matplotlib.pyplot as plt
image_url = "https://www.alleycat.org/wp-content/uploads/2019/03/FELV-cat.jpg"
response = requests.get(image_url)
# response.content 获取 HTTP 响应的原始二进制内容
print(response.content)
# BytesIO() 创建一个类文件对象,它操作内存中的字节缓冲区
# 将 response.content 传入 BytesIO() 创建一个包含图像数据的内存流
# 这样做允许 Image.open() 函数直接从内存中读取图像数据,而不需要先将其保存到磁盘。
image = Image.open(BytesIO(response.content)).convert('RGB')
plt.imshow(image)
plt.show()
二、结果