Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

image - python captcha decoder library

I need a Captcha decoder for python to read simple image captchas like the following picture:

captcha

simple captcha

captcha

Do you know of a library that can help me read this captcha?

If you don't know of a library for reading captchas, could you help me to read this (and others like this) with PIL?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I hope this captcha is not used anywhere.

Following is a dummy way to decode it. Basically what you need are the patterns from 0 to 9 as present in these captchas. From your examples, I have only the patterns for 0 3 4 5 7 8. Since everything is fixed on them, you know where to split each character. You also know each character is a number of fixed size and fixed font. If it also includes letters or more characters, but of fixed size and font, then the following code can be easily adapted.

What the code does is: a) load the patterns (I considered they are named n0.png, n1.png, ...); b) split the captcha in NUMS pieces; c) do a sum of squared differences between each pattern and each split number; d) decide that the the split number is the one with the smallest sum. It returns a list for each number, in order, present in the captcha. To obtain the initial patterns, you can uncomment the lines that save the split numbers, place a return after that piece, and adjust the file names.

import sys
from PIL import Image, ImageOps

PAT_SIZE = (8, 10)
NUMS = 3
FIRST_NUM_OFFSET = 5
NUM_OFFSET = (1, 3)


NUMBERS = []
for i in xrange(10):
    try:
        NUMBERS.append(Image.open('n%d.png' % i).load())
    except IOError:
        print "I do not know the pattern for the number %d." % i
        NUMBERS.append(None)


def magic(fname):
    captcha = ImageOps.grayscale(Image.open(fname))
    im = captcha.load()

    # Split numbers
    num = []
    for n in xrange(NUMS):
        x1, y1 = (FIRST_NUM_OFFSET + n * (NUM_OFFSET[0] + PAT_SIZE[0]),
                NUM_OFFSET[1])
        num.append(captcha.crop((x1, y1, x1 + PAT_SIZE[0], y1 + PAT_SIZE[1])))

    # If you want to save the split numbers:
    #for i, n in enumerate(num):
    #    n.save('%d.png' % i)

    def sqdiff(a, b):
        if None in (a, b): # XXX This is here just to handle missing pattern.
            return float('inf')

        d = 0
        for x in xrange(PAT_SIZE[0]):
            for y in xrange(PAT_SIZE[1]):
                d += (a[x, y] - b[x, y]) ** 2
        return d

    # Calculate a dummy sum of squared differences between the patterns
    # and each number. We assume the smallest diff is the number in the
    # "captcha".
    result = []
    for n in num:
        n_sqdiff = [(sqdiff(p, n.load()), i) for i, p in enumerate(NUMBERS)]
        result.append(min(n_sqdiff)[1])
    return result

print magic(sys.argv[1])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...