captchaimage 1.4 - Python extension to generate captcha images

2008, 2015 Fredrik Portstrom <https://portstrom.com>

I, the copyright holder of this file, hereby release it into the
public domain. This applies worldwide. In case this is not legally
possible: I grant anyone the right to use this work for any purpose,
without any conditions, unless such conditions are required by law.

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.

Freetype: http://freetype.org

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

See 'benchmark.py' and 'example.py'.
To see options, try: ./example.py --help

Below are two examples of how to generate a generate a JPEG image and
get it as a string. The code can be copied as is or modified to change
the font face and font size.

These functions randomly generate a random seed for distorting the
text every time they are called. If you allow displaying an image for
the same challenge multiple times, you can save the image data and
display the same image again, or save the random seed and generate an
image with the same random seed again.

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,
	random.randint(0, 2147483647))
    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,
        random.randint(0, 2147483647))
    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.4 (2015-04-07) - Added argument for random seed.
                 - Updated example.py to use optparse.
                 - Updated URL.

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 python-captchaimage to crash.

1.0 (2008-07-30) - Initial release.
