Generating a 3D CAPTCHA [pic]

后端 未结 4 1363
误落风尘
误落风尘 2020-12-28 09:05

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

Which

4条回答
  •  粉色の甜心
    2020-12-28 09:48

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

提交回复
热议问题