Commit ebe8e8d1 authored by Romain Bignon's avatar Romain Bignon

added the Captcha classes

parent 4ab2c519
# -*- coding: utf-8 -*-
Copyright(C) 2009 Romain Bignon
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import hashlib
import sys
import Image
class CaptchaError(Exception): pass
class TileError(Exception):
def __init__(self, msg, tile = None):
Exception.__init__(self, msg)
self.tile = tile
class Captcha:
def __init__(self, file):
self.inim =
self.nx,self.ny = self.inim.size
self.inmat = self.inim.load() = {}
self.tiles = [[Tile(x+5*y+1) for y in xrange(5)] for x in xrange(5)]
def __getitem__(self, (x, y)):
return self.inmat[x % self.nx, y % self.ny]
def all_coords(self):
for y in xrange(self.ny):
for x in xrange(self.nx):
yield x, y
def get_codes(self, code):
s = ''
for c in code:
s += '%02d' %[int(c)].id
return s
def build_tiles(self):
y = 5
ty = 0
while y < self.ny:
x = 6
tx = 0
while x < self.nx:
if self[x,y] == 8:
tile = self.tiles[tx][ty]
tile.valid = True
yy = y
while not self[x,yy] in (3,7):
l = []
xx = x
while not self[xx,yy] in (3,7):
xx += 1
yy += 1[tile.getNum()] = tile
x += 26
tx += 1
y += 25
ty += 1
class Tile:
hash = {'b2d25ae11efaaaec6dd6a4c00f0dfc29': 1,
'600873fa288e75ca6cca092ae95bf129': 2,
'da24ac28930feee169adcbc9bad4acaf': 3,
'76294dec2a3c6a7b8d9fcc7a116d1d4f': 4,
'd9531059e3834b6b8a97e29417a47dec': 5,
'8ba0c0cfe5e64d6b4afb1aa6f3612c1a': 6,
'19e0120231e7a9cf4544f96d8c388c8a': 7,
'83d8ad340156cb7f5c1e64454b66c773': 8,
'5ee8648d77eeb3e0979f6e59b2fbe66a': 9,
'3f3fb79bf61ebad096e05287119169df': 0
def __init__(self, _id): = _id
self.valid = False = []
def __repr__(self):
return "<Tile(%02d) valid=%s>" % (, self.valid)
def checksum(self):
s = ''
for pxls in
for pxl in pxls:
s += '%02d' % pxl
return hashlib.md5(s).hexdigest()
def getNum(self):
sum = self.checksum()
return self.hash[sum]
except KeyError:
raise TileError('Tile not found', self)
def display(self):
for pxls in
for pxl in pxls:
sys.stdout.write('%02d' % pxl)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment