Build your own website thumbnail generator with Django

On many website you can see a awesome link preview effect using a thumbnail of the page. There is some web services such as Websnapr or Thumbalizr providing an API to make your own webpages screenshots but it can be annoying to rely on third party projects: free accounts can only display a small among of screenshots and, cannot customize the size of the image, you cannot render Flash animations or Javascript…

Webkit Logo

We will build a website thumbnail generator using CutyCapt, a Webkit based free software designed to make screenshots of web pages, and Django, the powerful Python web framework.

The first step is to download and install CutyCapt. On a Debian-based system (such as Ubuntu) just type the following commands:

sudo apt-get install subversion libqt4-webkit libqt4-dev g++
svn co
cd cutycapt/CutyCapt
./CutyCapt --url=// --out=example.png

Try to open the example.png (i.e: eog example.png), if the install is OK you must see a screenshot of this blog.

I assume your Django installation is working fine. Start a new project if needed ( startproject mysite) and create an application called webthumb

with python startapp in your project.

Django Reinhardt

Edit your file to add a new pattern to the tuple urlpatterns. It will match screenshots requests and call the thumb view:

(r'^thumb/(?P.*)', 'mysite.webthumb.views.thumb'),

Now create a view called thumb in webthumb/ with this code:

import os
import subprocess
import md5
from django.http import HttpResponse

CUTYCAPT = '/home/dunglas/cutycapt/CutyCapt/CutyCapt'
THUMBS_DIR  = '/home/dunglas/Documents/Projets/kakofony/thumbs/'

def thumb(request, url):
    hash =
    path = THUMBS_DIR + hash + '.png'
    print path

    if not os.path.isfile(path):
                '--url=' + url,\
                '--out=' + path])
        except subprocess.CalledProcessError:
            return django.http.HttpResponseServerError

    img = open(path, 'rb').read()
    return django.http.HttpResponse(img, mimetype='image/png')

Adapt the variables CUTYCAPT and THUMBS_DIR with your settings. Of course the directory pointed by THUMBS_DIR must be writeable (chmod 777).

You can now embed website thumbnails in your websites just with this HTML markup <img src="http://localhost/thumb///" alt="Lapin Blanc" />.

Enjoy !

32 thoughts on “Build your own website thumbnail generator with Django

    1. hi I've got a problem using it with php it just doesn't work when I do for example
      system("./CutyCapt –url = –out=example.png");
      I've checked all the rights and everything is allright I've even tried with exec isntead of system…
      Have you got an idea?
      I think it éight be because of the time it takes to execute….

  1. Thanks for this – very helpful, and CutyCapt produces some great results. I'm just getting started with django. Is there an easy way to display some sort of "processing" message/graphic whilst CutyCapt does its thing, before redirecting to the captured image?

  2. Ah – I couldn't get it working on Intrepid so went back to Hardy. I've written mine in PHP, works an absolute treat. Only problem is flash – any ideas if/whether it works with flash?

  3. Hi. This blog post inspired me to try and make a web-snapshot thumbnailing service using Django and CutyCapt. It's my first Django project – so I'm sure there are lots of issues with it – and it's probably poorly coded!

    Anyway – thought you might be interested. I set up a project on Launchpad for it:

    What I have cobbled together as a test is available at: thumbnails can be generated using the format:


    It's quite slow though, and can't handle generating more than one capture at a time. Think I'll need to build some sort of queue for background capturing, and return a placeholder image.

  4. Is it rendering web-page that has adobe FLASH?????

    How can I render FLASH via CUTYCAPT.
    I've try many times…but I cannot do that…
    Is there any additional tasks for Flash????

  5. Is that a full list of dependencies?
    libqt4-webkit libqt4-dev g++
    I'm trying to set up under Redhat EL, and not having much joy.

  6. I tried to install cutycapt on OSX, i downloaded the source from the website, but it contain only 3 files: CutyCapt.cpp, CutyCapt.hpp, I run gmake but i had "No targets specified and no makefile found". Do you know how to intall CutyCapt on the mac?

  7. Pingback: Sansui
  8. >>> subprocess.check_call([‘xvfb-run –server-args=”-screen 0, 1024x768x24″‘, CUTYCAPT, ‘–url=’ + url, ‘–min-width=300’, ‘–out=’ + path])
    Traceback (most recent call last):
    File “”, line 1, in
    File “/usr/lib/python2.7/”, line 506, in check_call
    retcode = call(*popenargs, **kwargs)
    File “/usr/lib/python2.7/”, line 493, in call
    return Popen(*popenargs, **kwargs).wait()
    File “/usr/lib/python2.7/”, line 679, in __init__
    errread, errwrite)
    File “/usr/lib/python2.7/”, line 1249, in _execute_child
    raise child_exception
    OSError: [Errno 2] No such file or directory

    1. This error occured for my X server code . But executable is working fine.
      django code is correct or error ?
      THUMBS_DIR is 777 permission.

Leave a Reply