Last active
December 11, 2015 08:28
-
-
Save Phaeilo/4573411 to your computer and use it in GitHub Desktop.
Yet another solver for the MintEye captcha.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment