Wednesday, November 7, 2012

Easy read capcha with Python

I need crack some easy captcha, so i written one script to do that. I used PIL lib to edit images, convert background to whitem character to black. And Pytesster used to make OCR: Optical Character Recognition.

from PIL import Image

img = Image.open('a.jpeg') # Your image here!
img = img.convert("RGBA")
width, height = img.size
pixdata = img.load()

# Make the letters bolder for easier recognition

for y in xrange(img.size[1]):
    for x in xrange(img.size[0]):
        if pixdata[x, y][0] < 90:
            pixdata[x, y] = (0, 0, 0, 255)

for y in xrange(img.size[1]):
    for x in xrange(img.size[0]):
        if pixdata[x, y][1] < 136:
            pixdata[x, y] = (0, 0, 0, 255)

for y in xrange(img.size[1]):
    for x in xrange(img.size[0]):
        if pixdata[x, y][2] > 0:
            pixdata[x, y] = (255, 255, 255, 255)

img.save("input-black.gif", "GIF")

#   Make the image bigger (needed for OCR)
im_orig = Image.open('input-black.gif')
big = im_orig.resize((width, height), Image.NEAREST)

ext = ".tif"
big.save("input-NEAREST" + ext)

#   Pytesser
from pytesser import *
image = Image.open('input-NEAREST.tif')
print image_to_string(image)
------------------------------------------------------------
Thanks for reading
--------------------------------------------------------------------------
Security Research
All my Lab:
Linux Lab -- window and Cisco Lab
to be continued - I will update more.

No comments:

Install Xposed Inspector and Frida on Genymotion

Today i had some work with android. So i need trace application. I found 2 nice tool can help me: Xposed Inspector and Frida. To setup ther...