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 https://cutycapt.svn.sourceforge.net/svnroot/cutycapt
./CutyCapt --url=//dunglas.fr --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 (django-admin.py startproject mysite) and create an application called webthumb
with python manage.py startapp in your project.
Edit your urls.py file to add a new pattern to the tuple
urlpatterns. It will match screenshots requests and call the
Now create a view called
thumb in webthumb/view.py with this code:
from django.http import HttpResponse
CUTYCAPT = '/home/dunglas/cutycapt/CutyCapt/CutyCapt'
THUMBS_DIR = '/home/dunglas/Documents/Projets/kakofony/thumbs/'
def thumb(request, url):
hash = md5.new(url).hexdigest()
path = THUMBS_DIR + hash + '.png'
if not os.path.isfile(path):
'--url=' + url,\
'--out=' + path])
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 (
You can now embed website thumbnails in your websites just with this HTML markup
<img src="http://localhost/thumb///dunglas.fr" alt="Lapin Blanc" />.