public
Last active

Yet another solver for the MintEye captcha.

  • Download Gist
captcha.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import Image
import ImageChops
 
def diff_images(img_a, img_b):
# return sumOfAllPixelValues(abs(img_a - img_b))
return sum(map(sum, ImageChops.difference(img_a, img_b).getdata()))
 
def solve_captcha(filenames):
filenames = sorted(filenames)
images = map(lambda x: Image.open(x), filenames)
 
# precompute differences between all neighbouring images
pre_diffs = []
for i in range(len(images)-1):
left, right = images[i:i+2]
pre_diffs.append(diff_images(left, right))
 
# compare each image (except first and last) to its neighbours
diffs = {}
for i in range(len(pre_diffs)-1):
left, right = pre_diffs[i:i+2]
diffs[i+1] = left-right
 
# get extreme values
max_diff = max(diffs, key=diffs.get) # usually right of solution
min_diff = min(diffs, key=diffs.get) # usually left of solution
 
# solution somewhere in center
if min_diff == max_diff-2:
return filenames[min_diff+1]
 
# solution in the last two images, no max_diff
elif min_diff >= len(diffs)-2:
return filenames[min_diff+1]
 
# solution in the first two images, no min_diff
elif max_diff <= 2:
return filenames[max_diff-1]
 
# no solution
else:
return None
 
def main():
for captcha_num in range(17):
filenames = range(30)
filenames = map(lambda x: "%02d_%02d.jpg" % (captcha_num, x), filenames)
 
solution = solve_captcha(filenames)
if solution is None:
print "failed to solve %02d" % (captcha_num,)
else:
Image.open(solution).save("solved_%02d.jpg" % (captcha_num,))
print "solved %02d: %s" % (captcha_num, solution)
 
if __name__ == "__main__":
main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.