PDA

View Full Version : PIL filter problems..



Rambo
03-06-2014, 09:19 AM
[ 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:
[Only registered and activated users can see links]
to
[Only registered and activated users can see links]
.

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



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.[Only registered and activated users can see links](c j))
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 = '[Only registered and activated users can see links]' # 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:


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

DarkByte
03-06-2014, 10:31 AM
Edit:

Nm i didnt read that properly

Rambo
03-06-2014, 11:40 AM
If this helps, the first error I get is this:


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

Zachafer
03-07-2014, 12:47 AM
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 fileYour data_stream doesnt' have a valid image in it. Are there headers needing to be removed?

Rambo
03-07-2014, 08:54 AM
It turns out Zach, that I imported conflicting modules.