我有一个基于image.User上传图像的应用程序,如果它得到了管理员的批准,那么它就会显示在web上,page.These图像对于那些没有特定计划的用户来说是不可用的,.User必须购买一个计划,然后他/她可以下载那些images.But,我们知道有一个默认的选项可以用正确的click.So保存图像,我已经决定使用PIL在这些images.So上给出水印是安全的。
我在这里发现了一个图层,http://www.pythoncentral.io/watermark-images-python-2x/ .But不知道如何在我的django应用程序中实现它,因为我在python和django中非常新,上面的教程是针对单个image.But的,我有几个images.So,如何在我的所有图像中一次给出水印。我在我的views.py中使用这个定义在我的网页中显示图像。
def showimage(request,template = 'base.html',page_template = 'photo/showimage.html'):
photo_list = Photo.objects.all()
context = {}
context.update({
'photo_list': photo_list,
'page_template': page_template,
})
if request.is_ajax():
template = page_template
return render_to_response(template,context,context_instance=RequestContext(request)) 这是我的showimage.html,在这里我渲染了我的图像.
{% extends 'base.html'%}
{%block title%}{%endblock%}
{%block content%}
{% load endless %}
<div class="container" >
<div class="row mt" style="padding-top:0px; margin-top:10px;">
<ul class="grid effect-2" id="grid">
{% paginate 40 photo_list %}
{% for photo in photo_list%}
{% if photo.approved%}
<li><a href = "{% url 'download_image' photo.id %}">
<img src={{photo.photo.url}} alt = 'sample photo' /></a>
</li>
{%endif%}
{% endfor %}
</ul>
</div><!-- row -->
</div><!-- container -->
<p>{%show_more%}</p>
{%endblock%}没有必要只使用上面给定的链接,您也可以使用自己的代码修改我上面的给定视图。
发布于 2014-06-03 06:39:00
假设您拥有映像的服务器端url为: Photo.image_url。
您可以使用以下视图函数返回有水印的图像:
from PIL import Image
from django.core.servers.basehttp import FileWrapper
from django.http import StreamingHttpResponse
def render_image_with_watermark(request, pk, text):
# pk is the primary key of photo model
photo = get_object_or_404(Photo, pk=pk)
# watermark the photo and save it to a temp file
tmp_name = tempfile.mktemp()
# this function was introduced in:
# http://www.pythoncentral.io/watermark-images-python-2x/
add_watermark(photo.image_url, text, out_file=tmp_name, angle=23, opacity=0.25)
# render the watermarked photo to response
wrapper = FileWrapper(open(photo.image_url, 'rb'))
response = StreamingHttpResponse(wrapper, 'image/jpeg')
response['Content-Length'] = os.path.getsize(photo.image_url)
response['Content-Disposition'] = 'attachment; filename=photo.jpg'
return response如果您想用水印呈现所有图像,您可以首先使上面的render_image_with_watermark视图具有一个url:
# urls.py
urlpatterns = patterns('',
url(r'^photo/(?P<pk>\d+)/$', 'render_image_with_watermark', name='render_image_with_watermark'),
...
)完成此操作后,尝试访问url /photo/photo.pk,if成功,它将直接呈现图像。
然后,更改showimage.html模板:
{% extends 'base.html'%}
{%block title%}{%endblock%}
{%block content%}
{% load endless %}
<div class="container" >
<div class="row mt" style="padding-top:0px; margin-top:10px;">
<ul class="grid effect-2" id="grid">
{% paginate 40 photo_list %}
{% for photo in photo_list%}
{% if photo.approved%}
<li><a href = "{% url 'download_image' photo.id %}">
<!-- ATTENTION HERE -->
<img src={% url 'render_image_with_watermark' pk=photo.id %} alt = 'sample photo' /></a>
</li>
{%endif%}
{% endfor %}
</ul>
</div><!-- row -->
</div><!-- container -->
<p>{%show_more%}</p>
{%endblock%}试一试。
发布于 2014-08-06 18:35:51
免责声明:我是水印教程的作者
附加信息:对于网站输出,以下是更改:-
import os, sys, StringIO ## change line 2 ##
def add_watermark(in_file, angle=23, opacity=0.25): ## change line 6 ##
out_file = StringIO.StringIO() ## insert after line 6 ##
...
return out_file.getValue() ## insert after 24 ##使用它:myImage = add_watermark(photo_image)。使用myImage创建header和输出。基本上,我们将JPG保存到StringIO,并使用getValue()读取它。
也可以检查马克的叉子,而不是覆盖图像。
https://stackoverflow.com/questions/24008201
复制相似问题