Results 1 to 5 of 5

Thread: PIL filter problems..

  1. #1

    Rambo's Avatar
    Joined
    Sep 2013
    Posts
    136
    Userbars
    1
    Thanks
    19
    Thanked
    29/25
    DL/UL
    11/7
    Mentioned
    25 times
    Time Online
    4d 3h 14m
    Avg. Time Online
    1m

    PIL filter problems..

    [ EDIT: Turns out it was spelling errors. After fixing this and improving some code, i have a 10% success rate.. ]

    I am working on a marapets OCR, with the help of a friend. He told me to do this:

    1. Enhance Contrast

    2. Emboss

    3. Sobel Edge Filter

    4. A 3x3 Smooth Filter

    4. Grayscale

    5. Then a 4x4 avg threshold, values under 75 are black, otherwise white.

    To get from:
    (you need an account to see links)
    to
    (you need an account to see links)
    .

    So I wrote this code, but I don't get the desired output.

    Code:
    from PIL import Image,ImageEnhance,ImageTk,ImageFilter
    from easygui import *
    from pytesser import *
    
    import urllib,urllib2
    import cookielib,io
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders = [
    ('User-Agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11'),
    ]
    url = 'http://www.marapets.com/codes/code.php' # Url of test captcha
    image_bytes = opener.open(url).read()
    # internal data file
    data_stream = io.BytesIO(image_bytes)
    # open as a PIL image object
    pil_image = Image.open(data_stream).save("CaptchaCode.png") # save to png
    image = Image.open("CaptchaCode.png") # load 
    enhancer = ImageEnhance.Sharpness(image)
    
    factor = 0.2
    enhancer.enhance(factor).save("demo2.png")
    imagex = Image.open("demo2.png")
    imagex = imagex.filter(ImageFilter.EMBOSS)
    imagex = imagex.filter(ImageFilter.SMOOTH)  
    imagex = imagex.filter(ImageFilter.EDGE_ENHANCE)
    imagex = imagex.convert("L")
    imagex = imagex.point(lambda x: 0 if x<140 else 255, '1')
    imagex.save("demo2.png")
    image2="demo2.png"
    x = Image.open("demo2.png").save("demo2.gif","GIF")
    z = image.open("demo2.gif")
    image_to_string(z)
    reply = enterbox(msg='Would you like to buy: ', title='Enter Security Code', default='', strip=False,root=None,image=image2)
    if reply:
        coder=reply.replace(" ","")
        if coder == "":
            print "Item Skipped"
    What am I doing wrong?

    EDIT 1:

    If this helps, the first error I get is this:
    Code:
    Traceback (most recent call last):
      File "C:\Users\NCS Customer\Desktop\Batch\Tesseract\test.py", line 17, in <module>
        pil_image = Image.open(data_stream).save("CaptchaCode.png")
      File "C:\Python27\lib\site-packages\PIL\Image.py", line 2025, in open
        raise IOError("cannot identify image file")
    IOError: cannot identify image file
    Last edited by Rambo; 03-06-2014 at 05:06 PM. Reason: more details

  2. #2

    Joined
    Jun 2012
    Posts
    1,699
    Thanks
    876
    Thanked
    2,881/1,142
    DL/UL
    44/1
    Mentioned
    562 times
    Time Online
    118d 6h 45m
    Avg. Time Online
    40m
    Edit:

    Nm i didnt read that properly
    Last edited by DarkByte; 03-06-2014 at 10:33 AM.

  3. #3

    Rambo's Avatar
    Joined
    Sep 2013
    Posts
    136
    Userbars
    1
    Thanks
    19
    Thanked
    29/25
    DL/UL
    11/7
    Mentioned
    25 times
    Time Online
    4d 3h 14m
    Avg. Time Online
    1m
    If this helps, the first error I get is this:
    Code:
    Traceback (most recent call last):
      File "C:\Users\NCS Customer\Desktop\Batch\Tesseract\test.py", line 17, in <module>
        pil_image = Image.open(data_stream).save("CaptchaCode.png")
      File "C:\Python27\lib\site-packages\PIL\Image.py", line 2025, in open
        raise IOError("cannot identify image file")
    IOError: cannot identify image file

  4. #4
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m
    Code:
    Traceback (most recent call last):
      File "C:\Users\NCS Customer\Desktop\Batch\Tesseract\test.py", line 17, in <module>
        pil_image = Image.open(data_stream).save("CaptchaCode.png")
      File "C:\Python27\lib\site-packages\PIL\Image.py", line 2025, in open
        raise IOError("cannot identify image file")
    IOError: cannot identify image file
    Your data_stream doesnt' have a valid image in it. Are there headers needing to be removed?

  5. #5

    Rambo's Avatar
    Joined
    Sep 2013
    Posts
    136
    Userbars
    1
    Thanks
    19
    Thanked
    29/25
    DL/UL
    11/7
    Mentioned
    25 times
    Time Online
    4d 3h 14m
    Avg. Time Online
    1m
    It turns out Zach, that I imported conflicting modules.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •