如何使用PIL Image在Django网页中输出图像 [英] how to output a Image in Django web page with PIL Image
本文介绍了如何使用PIL Image在Django网页中输出图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Image.new在django视图中创建一个图像对象,
我想在网页中显示。
x = Image.new( 'RGB',(400,400))
return HttpResponse(x.show(),mimetype =image / png)
/ p>
如何将图像对象隐藏到图像原始二进制文件?
解决方案
p>您有两个选项:
- 将您的图像保存在Web服务器可以在其中服务的位置
- 使用base64编码并直接显示
第一个选项是首选项。代码应该是这样的:
x = Image.new()#与你的
x相同的东西。保存(MEDIA_ROOT +/generated_images/the_name_of_the_image.jpg,JPEG)
return HttpResponse(
< img src =%s /%s />%(MEDIA_URL, generate_images / the_name_of_the_image.jpg)
)
如果你愿意, base64(参见:使用base64编码图像文件)
和显示它:
base64_img = get_base_64()
return HttpResponse('< img alt =Embedded Imagesrc =data:image / jpeg; base64%base64_img />')
I use Image.new create a image object in a django view, I want to show it in webpages.
x = Image.new('RGB',(400,400)) return HttpResponse(x.show(), mimetype="image/png")
doesn't work.
How can I covert a image object to a image raw binary?
解决方案
You have 2 options:
- Save your image in some place your web server can serve it
- Encode it with base64 and show it directly
The first option is the prefered one. The code should be something like:
x = Image.new() # The same stuff than yours
x.save(MEDIA_ROOT + "/generated_images/the_name_of_the_image.jpg", "JPEG")
return HttpResponse(
"<img src="%s/%s />" % (MEDIA_URL, "/generated_images/the_name_of_the_image.jpg")
)
If you want, you can read that in base64 (see: Encoding an image file with base64) And display it:
base64_img = get_base_64()
return HttpResponse('<img alt="Embedded Image" src="data:image/jpeg;base64," % base64_img />')
这篇关于如何使用PIL Image在Django网页中输出图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文