Django 上传图片打水印的步骤

0 投票
1 回答
1087 浏览
提问于 2025-04-17 20:29

我正在使用django watermark来给我网站上的图片加水印。我已经按照这个链接完成了所有的设置。

下面是我的settings.py文件(这里只显示了与水印相关的更改)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django.contrib.admin',

    'photo',
    'userena',
    'guardian',
    'easy_thumbnails',
    'accounts',

    'paypal.standard.ipn',
    'myprofile',
    'watermarker',

)

WATERMARKING_QUALITY = 85
WATERMARK_OBSCURE_ORIGINAL = False

根据上面的链接,我在数据库的水印表中上传了一张透明的PNG图片,名字叫Medical Art,现在我在下面的模板中应用了这个水印...

{% extends 'base.html'%}
{% load watermark %}
{% block title %}ShutterStock{%endblock%}


{% block content %}



 <ul> 

{% for photo in photo_list%}
  <li><a href = "{% url 'download_image' photo.id %}"><img src={{photo.photo.url|watermark: 'Medical Art,opacity=40,tile=1'}} alt = 'sample photo' width = '200' height = '200'/></li></a>
{% endfor %}
</ul>





{% endblock %}

在提到的地方,我展示的图片就是我上传的那张图片。但是在运行网站后,我遇到了以下错误:

IOError at /showphoto/

解码器zip不可用

1 个回答

0

去掉filter和参数之间的空格:

{{photo.photo.url|watermark: 'Medical Art,opacity=40,tile=1'}}
                            ^

举个例子:

  • 没有空格的情况:

    >>> from django.template import Template, Context
    >>> Template('''{{nothing|default:'HELLO'}}''').render(Context({}))
    u'HELLO'
    
  • 有空格的情况:

    >>> Template('''{{nothing|default: 'HELLO'}}''').render(Context({}))
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/home/falsetru/.virtualenvs/django16/local/lib/python2.7/site-packages/django/template/base.py", line 125, in __init__
        self.nodelist = compile_string(template_string, origin)
      File "/home/falsetru/.virtualenvs/django16/local/lib/python2.7/site-packages/django/template/base.py", line 153, in compile_string
        return parser.parse()
      File "/home/falsetru/.virtualenvs/django16/local/lib/python2.7/site-packages/django/template/base.py", line 254, in parse
        filter_expression = self.compile_filter(token.contents)
      File "/home/falsetru/.virtualenvs/django16/local/lib/python2.7/site-packages/django/template/base.py", line 360, in compile_filter
        return FilterExpression(token, self)
      File "/home/falsetru/.virtualenvs/django16/local/lib/python2.7/site-packages/django/template/base.py", line 572, in __init__
        self.args_check(filter_name, filter_func, args)
      File "/home/falsetru/.virtualenvs/django16/local/lib/python2.7/site-packages/django/template/base.py", line 641, in args_check
        (name, len(nondefs), plen))
    TemplateSyntaxError: default requires 1 arguments, 0 provided
    

撰写回答