【发布时间】:2014-09-18 08:45:05
【问题描述】:
我已使用以下代码将图像转换为 base64 字符串。基于此字符串,想要创建 Image 对象并希望向其添加一些标头,例如,
到期:2013 年 4 月 29 日星期一 21:44:55 GMT Cache-Control: max-age=0, no-cache, no-store ,
并将该对象作为图像打印到 django 模板。
在 django 中可以吗?
这是将img从url转换为base64的代码。
import mimetypes
import urllib2
def stringifyImage(url):
tmpdir = 'tmp'
mmtp = mimetypes.guess_type(url, strict=True)
if not mmtp[0]:
return False
ext = mimetypes.guess_extension(mmtp[0])
f = open(tmpdir+'tmp'+ext,'wb')
f.write(urllib2.urlopen(url).read())
f.close()
img = open(tmpdir+'tmp'+ext, "rb").read().encode("base64").replace("\n","")
return img
【问题讨论】:
-
所以当你说将图像对象添加到 Django 时,“只是澄清一下”,你还想显示你改变了标题的图像吗?
-
@LearningNeverStops 是的
-
猜测:所以您仍然可以取回图像对象并通过上下文将其传递给httpresponse,然后可以通过模板访问该对象对吗?您是否尝试过为 html 的 src 属性访问相同的属性?
-
那么你让 HttpResonse 工作了吗? :)
标签: python django image python-imaging-library