Feature request : Captcha

If you’re feeling brave … :slight_smile:

I started putting this together as a proof of concept, but it’s very, very far from working code. So far it generates text as an image and returns it to be shown on the form. It’s meant to turn it into a 3D plot but I think a special version of matplotlib is required. I was intending to try an alternative but didn’t get very far.

But for what it’s worth, here’s what I was starting to do :

Create Form1 with Image & Button controls
Add this to the Button click event :

  def button_generate_image_click (self, **event_args):
    # This method is called when the button is clicked
    r=anvil.server.call('generate_captcha')
    
    i = self.image_1
    i.source=r['image']

Next, create a server module with this in it :

import tables
from tables import app_tables
import anvil.server
import io

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

@anvil.server.callable
def generate_captcha():
  m=anvil.Media()
 
  sz = (50,30) 
  img = Image.new('L', sz, 255)
  drw = ImageDraw.Draw(img)
  font = ImageFont.load_default().font  
  drw.text((5,3), 'text', font=font)
  bs = io.BytesIO()
  img.save(bs,format="png")
  
  row=app_tables.captcha.get(id=1)
  row.update(image=anvil.BlobMedia("image/png",bs.getvalue()))
  return row

# This bit being problematic.
#  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')

Create Data Tables with a table called “captcha”,and two fields : id & image.

Clicking the button will create an image with the word “text” in it, put it in a Data Tables table and return it to the form for displaying in an image control.

Obviously, lots to do (actually obfuscate it some how, actually capture the text from the user and compare it, etc etc.

Sources :

2 Likes