django中使用PIL的图像水印(image watermark using PIL in django)

编程入门 行业动态 更新时间:2024-10-22 18:42:35
django中使用PIL的图像水印(image watermark using PIL in django)

我有一个基于image.User上传图像的应用程序,如果它被管理员批准,那么它显示在网页中。这些图像不适用于那些没有具体计划的用户。用户必须购买计划和然后他/她可以下载那些图像。但我们知道有默认选项可以通过右键单击保存图像。所以在这些图像上给出水印是安全的。所以我决定使用PIL给出水印。

我在这里发现了一个turorial http://www.pythoncentral.io/watermark-images-python-2x/ 。但不知道如何在我的django应用程序中实现它,因为我是python和django的新手。上面的教程是对于单个图像。但我有几个图像。所以如何在我的所有图像中一次给出水印。 我在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%}

它不必仅使用上面给出的链接,你也可以使用自己的代码修改我上面给出的视图。

i have an application which based on image.User upload images and if it is approved by the admin then it shows in the web page.These images are not available for those user who are not in specific plan.User has to buy a plan and then he/she can download those images.But we know that there is default option to save images with right click.So it will be safe to give a watermark on those images.So i have decided to give watermark using PIL.

I have found a turorial here http://www.pythoncentral.io/watermark-images-python-2x/ .But not sure how to implement it in my django app since i am very new in python and django.The above tutorial is for a single image.But i have several images.So how to give watermark in all of my images at a time. i am using this definition in my views.py to show images in my web page.

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))

and here is my showimage.html where i have rendered my images...

{% 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%}

its not necessary to use the above given link only,you can modify my above given view using your own code too.

最满意答案

假设您有图像的服务器端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 ,如果成功,它将直接渲染图像。

然后,更改您的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%}

试试。

Assume you have the server-side url of image as: Photo.image_url

You can use the following view function to return an watermarked image:

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

In case you want to render all images with watermarked, you can first make the render_image_with_watermark view above has a url:

# urls.py urlpatterns = patterns('', url(r'^photo/(?P<pk>\d+)/$', 'render_image_with_watermark', name='render_image_with_watermark'), ... )

After doing this, try to visit the url /photo/photo.pk,if success, it will render the image directly.

Then, change your showimage.html template:

{% 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%}

Have a try.

更多推荐

本文发布于:2023-08-04 08:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:水印   图像   PIL   django   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!