如何应用Thumbor滤镜效果

时间:2014-03-18 03:40:44

标签: python django thumbor

我尝试使用Thumbor对编码后的网址应用灰度滤镜效果,但没有成功。

我已经通过Django中的以下代码成功创建了缩略图。两个视图都返回缩略图。

views.py
--------

from django.shortcuts import render
from django_thumbor import generate_url, conf
from django.http import HttpResponse
from libthumbor import CryptoURL
from django.conf import settings

def index(request):
    resized = generate_url("/media/image.jpg", width=100)

    return render(request, 'webthumb/index.html', {'thumbnail':resized})

def crypto(request):
    base = settings.THUMBOR_SERVER
    crypto = CryptoURL(key=settings.THUMBOR_SECURITY_KEY)

    thumbor_kwargs = {}
    thumbor_kwargs['image_url'] = 'localhost:8000/media/image.jpg'
    thumbor_kwargs['width'] = 80

    encrypted_url = crypto.generate(**thumbor_kwargs)

    new_image = {'thumbnail':u'{}{}'.format(base,encrypted_url)}
    return render(request, 'webthumb/index.html', new_image)

模板文件:

<!DOCTYPE html>
{% load thumbor_tags %}
<html>

    <head>
        <title>Thumbor</title>
    </head>

    <body>
        <h1>Test</h1>      
        hello world! <strong>{{ boldmessage }}</strong><br /> 
        <img src={{thumbnail}} />
    </body>
</html>

如何应用类似于Pollexor中使用Java客户端进行Thumbor图像服务的图像效果?

1 个答案:

答案 0 :(得分:1)

只需使用filters参数:

thumbor_kwargs = {}
thumbor_kwargs['image_url'] = 'localhost:8000/media/image.jpg'
thumbor_kwargs['width'] = 80
thumbor_kwargs['filters'] = [
    "quality(80)",
    "grayscale()",
]

然后滤镜将应用于图像中。