12
votes

I would like to write a Python script that would generate a 3D CAPTCHA like this one: teabag captcha

Which graphics libraries can I use?

Source: ocr-research.org.ua

4
Just another question: is 3d captcha harder to ocr? - RMAAlmeida
Much much harder. Read the 'source' for more info. - RobertW
Oh no, did someone really call it teabag?? - Jim Buck
Yes. And they actually sell this to big companies. - RobertW

4 Answers

33
votes

There are many approaches. I would personally create the image in Python Imaging Library using ImageDraw's draw.text, convert to a NumPy array (usint NumPy's asarray) then render with Matplotlib. (Requires Matplotlib maintenance package).

Full code (in 2.5):

import numpy, pylab
from PIL import Image, ImageDraw, ImageFont
import matplotlib.axes3d as axes3d

sz = (50,30)

img = Image.new('L', sz, 255)
drw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", 20)

drw.text((5,3), 'text', font=font)
img.save('c:/test.png')

X , Y = numpy.meshgrid(range(sz[0]),range(sz[1]))
Z = 1-numpy.asarray(img)/255

fig = pylab.figure()
ax = axes3d.Axes3D(fig)
ax.plot_wireframe(X, -Y, Z, rstride=1, cstride=1)
ax.set_zlim((0,50))
fig.savefig('c:/test2.png')

alt text

Obviously there's a little work to be done, eliminating axes, changing view angle, etc..

4
votes

Another binding to consider for rendering with opengl is pyglet. Its best feature is that it is just one download. I think it contains everything you need to implement what Anurag spells out.

I will caution you that what you're trying to do is not exactly a simple first project in 3d graphics. If this is your first exposure to OpenGL, consider a series of tutorials like NeHe Tutorials and other help from the OpenGL website.

2
votes

I'm not sure I would bother with a full 3D library for what you have above. Just generate a matrix of 3D points, generate the text with something like PIL, scan over it to find which points on the grid are raised, pick a random camera angle and then project the points into a 2D image and draw them with PIL to the final image.

That being said... you may be able to use VPython if you don't want to do the 3D math yourself.

1
votes

Use Python bindings for OpenGL, http://pyopengl.sourceforge.net/.

Create a 2D image of white color text over a black surface using PIL. Make a 3D grid from this, increase z of point where color is white, maybe set z=color value, so by blurring the image you can get real curves in the z direction.

Create an OpenGL triangle from these points, use wireframe mode while rendering.

Grab the OpenGL buffer into an image, for example, http://python-opengl-examples.blogspot.com/2009/04/render-to-texture.html.