如何在模板中显示PIL Image对象? [英] How do I display a PIL Image object in a template?

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

问题描述

如果用户上传图像,并且我使用PIL调整大小,我得到一个 PIL Image对象

If a user uploads an image, and I resize it using PIL, I get a PIL Image object.

如何在模板中显示一个 PIL Image 文件它已经保存到数据库中?甚至可以以图像形式传递,并呈现?

How do I display a PIL Image file in a template, before it has been saved to the database? Can it even be passed in as an image and rendered?

推荐答案

对于有限的一组浏览器,您可以对图像进行base64编码并使用内联图像。请参阅嵌入Base64图像

For a limited set of browsers, you can base64 encode the image and use inline images. See Embedding Base64 Images.

解决方案适用于所有浏览器的是引用视图,返回图像的图像标记。

A solution that works for all browsers is an image tag referencing a view that returns the image.

[更新]


我想要的是为用户提交原始图像,然后由另一个表单提示输入图像的标题(图像字幕左侧的已调整大小的图像)。然后当用户点击提交图像,并将标题保存在模型实例中。

All I want is for the user to submit the original image, and then be prompted by another form to input a caption for the image (with the resized image to the left of the caption field). Then when the user hits "submit" the image and the caption get saved in a model instance.

嗯...当你使用< img src =foo> ,foo总是被GET检索,也许这就是为什么它不工作 - request.FILES在GET请求中不可用。如果您打开firebug或chrome调试工具栏,在网络选项卡中,您将看到上传的图像的POST请求,之后是获取图像的GET请求。

Well... When you use <img src="foo">, foo is always retrieved by a GET perhaps that is why it is not working - request.FILES will not be available in a GET request. If you open firebug or the chrome debug toolbar, in the network tab, you will see the POST request with the uploaded image and after that a GET request to fetch the image.

您必须将图像保存在两个步骤之间。

You have to save the image somewhere between both steps.


我还可以保存吗?我会喜欢它是暂时的。你认为有一个很简单的方法可以做到这一点,还是应该去看看这些选项?

how else could i save it? I would love for it to be temporary. Do you think there's a really easy way to do this, or should I go look into those options?

热门选择是 redis memcached 。您可以将它们视为具有过期日期的巨型共享python dict。如果图像很小,像头像,还可以将图像数据保存在会话变量中。

Popular choices are redis and memcached. You can think of them as giant shared python dict with an expire date. If the images are small, like an avatar, you can also save the image data in a session variable.

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

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