Advertisement
AbsoluteGamer

Captcha Solver

Aug 28th, 2017
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. # for http://www.boomlings.com/database/accounts/accountManagement.php captcha's
  2.  
  3. from PIL import Image
  4.  
  5. chars = {36 : '0',
  6.          27 : '1',
  7.          32 : '2',
  8.          31 : '3',
  9.          35 : '4',
  10.          37 : '5',
  11.          39 : '6',
  12.          26 : '7',
  13.          40 : '8',
  14.          39 : '9'}
  15.  
  16. def unrandomise(cpa):
  17.     img = Image.new('RGB',cpa.size)
  18.     for x in range(cpa.size[0]): # all pixels in the captcha are grey.
  19.         for y in range(cpa.size[1]):
  20.             if (cpa.getpixel((x,y))[0] <= 0x20):
  21.                 img.putpixel((x,y),(0,0,0))
  22.             elif (cpa.getpixel((x,y))[0] >= 0xe0):
  23.                 img.putpixel((x,y),(0xff,0xff,0xff))
  24.             else:
  25.                 img.putpixel((x,y),(0xff,0,0))
  26.     return img
  27.  
  28. def split_num(im,index):
  29.     box = (5+(index*9),im.getbbox()[1],4+((index+1)*9),im.getbbox()[3])
  30.     return im.crop(box)
  31.  
  32. def read_num(split_im):
  33.     t = 0
  34.     for x in range(split_im.size[0]):
  35.         for y in range(split_im.size[1]):
  36.             if (split_im.getpixel((x,y)) == (255,255,255)):
  37.                 t += 1
  38.     if (t == 39):
  39.         if (split_im.getpixel((7,3)) == (255,255,255)):
  40.             return '9'
  41.         return '6'
  42.     else:
  43.         return chars[t]
  44.  
  45. def read_captcha(im):
  46.     img = unrandomise(im)
  47.     res = ''
  48.     for i in range(5):
  49.         res += read_num(split_num(img,i))
  50.     return res
  51.  
  52. file_path = 'captcha.jpg'
  53. captcha = Image.open(file_path)
  54. print(read_captcha(captcha))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement