Generating a 3D CAPTCHA [pic]

浪尽此生 提交于 2019-11-30 03:48:23
Paul

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')

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

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.

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.

Anurag Uniyal

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!