Free software projects:

  • captchaimage

    Python extension to generate captcha images

  • oggvideopreview

    Python extension to get preview images from Theora videos in Ogg files

  • v4l2capture

    Python extension to capture video with video4linux2

  • Sinatra

    Karaoke game

Web projects (Swedish):

email: fredrik@jemla.se

Windows 7 Sins

python-captchaimage

Python extension to generate captcha images

Source code: captchaimage-1.3.tar.gz (4 kB)

License: Public domain. See notice in each file.

Email: fredrik@jemla.se

Example image:

Introduction

python-captchaimage is a fast and easy to use Python extension for creating images with distorted text that are easy for humans and difficult for computers to read. Glyphs are loaded as bezier curves using Freetype, rotated and scaled randomly, and then distorted by adding Perlin noise to each point of the curve before rendering into a bitmap. python-captchaimage generates about 950 images a second on a 1800 MHz Intel Celeron.

Installation

python-captchaimage uses distutils.
python-captchaimage requires the library Freetype 2. http://freetype.org
To build: ./setup.py build
To build and install: ./setup.py install

Example

To get a string containing a JPEG formatted image: It as a number of lines, but it's just to copy and paste, and maybe change the font face.

import captchaimage
import cStringIO
import Image

# Black text on white background
def get_captcha_image(code):
    size_y = 32
    image_data = captchaimage.create_image(
        "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 28, size_y, code)
    file = cStringIO.StringIO()
    Image.fromstring(
        "L", (len(image_data) / size_y, size_y), image_data).save(
        file, "JPEG", quality = 30)
    return file.getvalue()

# Arbitrary colors, in the formats PIL accepts
def get_color_captcha_image(code, background_color, text_color):
    size_y = 32
    image_data = captchaimage.create_image(
        "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 28, size_y, code)
    size_x = len(image_data) / size_y
    file = cStringIO.StringIO()
    mask_im = Image.fromstring("L", (size_x, size_y), image_data)
    target_im = Image.new("RGB", (size_x, size_y), text_color)
    target_im.paste(background_color, (0, 0), mask_im)
    target_im.save(file, "JPEG", quality = 30)
    return file.getvalue()

# Example: Magenta text on yellow background
# get_color_captcha_image("TESTING", (255, 255, 0), (255, 0, 255))

Change log

  • 1.3 (2010-02-06) - Added example with color to README file.
  • 1.2 (2009-11-03) - Fixed reference to the wrong variable. Updated URL.
  • 1.1 (2008-09-13) - Fixed bug causing captchaimage to crash.
  • 1.0 (2008-07-30) - Initial release.