title: Captcha Break or Text reader from Image: OCR-Python
body: <p>I have a typical captcha image which contain only digits.</p>
  
  <p>Ex. <a href="https://i.stack.imgur.com/PzTzu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PzTzu.png" alt="enter image description here"></a></p>
  
  <p>i want to extract <strong>78614</strong> from this image.</p>
  
  <p>I tried few library &amp; code using OCR-Python. But its returning 0.</p>
  
  <p><strong>Sample Code-1</strong></p>
  
  <pre><code>from captcha_solver import CaptchaSolver
  
  solver = CaptchaSolver('browser')
  with open('captcha.png', 'rb') as inp:
  raw_data = inp.read()
  print(solver.solve_captcha(raw_data))
  </code></pre>
  
  <p><strong>Sample Code-2</strong></p>
  
  <pre><code>from PIL import Image
  
  def p(img, letter):
      A = img.load()
      B = letter.load()
      mx = 1000000
      max_x = 0
      x = 0
      for x in range(img.size[0] - letter.size[0]):
          _sum = 0
          for i in range(letter.size[0]):
              for j in range(letter.size[1]):
                  _sum = _sum + abs(A[x+i, j][0] - B[i, j][0])
          if _sum &lt; mx :
              mx = _sum
              max_x = x
      return mx, max_x
  
  
  def ocr(im, threshold=200, alphabet="0123456789abcdef"):
      img = Image.open(im)
      img = img.convert("RGB")
      box = (8, 8, 58, 18)
      img = img.crop(box)
      pixdata = img.load()
  
      letters = Image.open(im)
      ledata = letters.load()
  
      # Clean the background noise, if color != white, then set to black.
      for y in range(img.size[1]):
          for x in range(img.size[0]):
              if (pixdata[x, y][0] &gt; threshold) \
                      and (pixdata[x, y][1] &gt; threshold) \
                      and (pixdata[x, y][2] &gt; threshold):
  
                  pixdata[x, y] = (255, 255, 255, 255)
              else:
                  pixdata[x, y] = (0, 0, 0, 255)
  
      counter = 0;
      old_x = -1;
  
      letterlist = []
  
      for x in range(letters.size[0]):
          black = True
          for y in range(letters.size[1]):
              if ledata[x, y][0] &lt;&gt; 0 :
                  black = False
                  break
          if black :
              if True :
                  box = (old_x + 1, 0, x, 10)
                  letter = letters.crop(box)
                  t = p(img, letter);
                  print counter, x, t
                  letterlist.append((t[0], alphabet[counter], t[1]))
              old_x = x
              counter += 1
  
      box = (old_x + 1, 0, 140, 10)
      letter = letters.crop(box)
      t = p(img, letter)
      letterlist.append((t[0], alphabet[counter], t[1]))
  
      t = sorted(letterlist)
      t = t[0:5]  # 5-letter captcha
  
      final = sorted(t, key=lambda e: e[2])
      answer = ""
      for l in final:
          answer = answer + l[1]
      return answer
  
  print(ocr('captcha.png'))
  </code></pre>
  
  <p>Has anyone had the opportunity to get/extract text from such typical captcha?</p>