【发布时间】:2021-09-01 10:33:53
【问题描述】:
我正在从 api 获取图像并在 django 应用程序中呈现。以这种方式加载图像真的很慢。
模板标签
def url(id):
url = f'http://api.com/image?id={id}'
response = requests.get(url)
img = Image.open(BytesIO(response.content))
data = BytesIO()
img.save(data, "PNG")
data.seek(0)
encoded_img_data = base64.b64encode(data.getvalue())
return encoded_img_data.decode('utf-8')
html
<img src="data:image/png;base64,{{row.image_md5|url}}" />
它可以工作,但加载图像需要 1 秒。有没有办法以这种方式更快地加载所有图像?
【问题讨论】:
标签: python django image performance python-imaging-library