Search code examples
pythondjangothumbor

How to apply Thumbor filters effects


I'm trying to apply the grayscale filter effect on an encoded URL using Thumbor without success.

I've successfully created the thumbnail images via the following code in Django. Both views return the thumbnail images.

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)

And the template file:

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

How do I apply image effects similar to those in Pollexor which uses Java client for the Thumbor image service ?


Solution

  • Just use the filters argument:

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

    Then the filters will be applied in the image.