如何在Django中显示内存中的图片? [英] How to display picture from memory in Django?

查看:37
本文介绍了如何在Django中显示内存中的图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用以下方式从内存中将图片显示为页面:

I know how to show picture as a page from memory just using like this:

import cStringIO

mStream = cStringIO.StringIO(picBin)

return HttpResponse(mStream.getvalue(),"image/jpg")

但是,如果我不想将图片显示为页面,而是想在页面中显示,例如使用HTML,有人知道如何设置图片"src"怎么办?是从内存加载的?

But what if I don't want to show the picture as a page, instead I want to show it within a page, say using in HTML, does someone has an idea what I should set the "src" if the picture is loaded from memory?

推荐答案

您需要对图像内容进行base-64编码到数据URI :

You'll need to base-64 encode the image contents into a Data URI:

data_uri = 'data:image/jpg;base64,'
data_uri += mStream.getvalue().encode('base64').replace('\n', '')

现在,您可以将 data_uri 推入图像的 src 属性中.

Now you can shove data_uri into the src attribute of an image.

这篇关于如何在Django中显示内存中的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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