I. Preparation and code examples
1, PIL, Pytesser, tesseract
(1) Install pil::http://www.pythonware.com/products/pil/(csdn download)
After the download is an EXE, directly double-click the installation, it will automatically install into the C:\Python27\Lib\site-packages,
(2) pytesser::http://code.google.com/p/pytesser/, (csdn download)
After downloading the decompression, put C:\Python27\Lib\site-packages directly (depending on the Python path you installed), and create a new Pytesser. PTH, the content is written Pytesser, Note that the content here must be the same name as the Pytesser folder, meaning that the Pytesser folder, Pytesser.pth, and content are the same!
(3) Tesseract OCR engine Download: http://code.google.com/p/tesseract-ocr/(csdn download)
After downloading, unzip, Tessdata folder, use it to replace the Pytesser extracted Tessdata folder. (On the Pytesser folder above)
Second, verification
(1) Principle:
Verification Code Image Processing
The Verification code image recognition technology is mainly to manipulate the pixels within the image, through a series of operations on the pixel points of the image, and finally output the text matrix of each character within the captcha image.
1. Read the picture
2. Picture noise reduction
3. Picture Cutting
4. Image text output
(2) Verifying character recognition
The character recognition in the verification code is mainly based on the machine learning classification algorithm, at present I use the algorithm of character recognition is KNN (k proximity algorithm) and SVM (Support vector machine algorithm), I will be the two algorithms in detail in the application of the scenario.
1. Get the character matrix
2, matrix into the classification algorithm
3. Output results
The picture to verify is as follows:
(3), simple command:
[Python]View PlainCopy
- From Pytesser Import *
- Image = Image.open (' 1.jpg ') # Open Image object using PIL
- Print image_to_string (image) # Run Tesseract.exe on image
Then run:
or directly:
[HTML]View PlainCopy
- Print image_file_to_string (' fnord.tif ')
You can also output the results!
(4), a bit more complicated
The above only for some relatively simple to do the processing, a
Principle: Color to grayscale, gray to two value, binary image recognition
[Python]View PlainCopy
- # Verification Code identification, this program can only identify data verification code
- Import Image
- Import Imageenhance
- Import ImageFilter
- Import Sys
- From Pytesser Import *
- # Binary Value
- Threshold =
- Table = []
- For I in range:
- If I < threshold:
- Table.append (0)
- Else:
- Table.append (1)
- #由于都是数字
- #对于识别成字母的 Use this table for corrections
- rep={' O ':' 0 ',
- ' I ':' 1 ',' L ':' 1 ',
- ' Z ':' 2 ',
- ' S ':' 8 '
- };
- def getverify1 (name):
- #打开图片
- im = Image.open (name)
- #转化到灰度图
- Imgry = Im.convert (' L ')
- #保存图像
- Imgry.save (' G ' +name)
- #二值化, the threshold segmentation method is used to threshold the segmentation point .
- out = imgry.point (table,' 1 ')
- Out.save (' B ' +name)
- #识别
- Text = image_to_string (out)
- #识别对吗
- Text = Text.strip ()
- Text = Text.upper ();
- For R in Rep:
- Text = Text.replace (R,rep[r])
- #out. Save (text+ '. jpg ')
- Print Text
- return text
- Getverify1 (' 1.jpg ') #注意这里的图片要和此文件在同一个目录, or you can pass an absolute path.
Post-run Effects:
from:http://blog.csdn.net/evankaka/article/details/49533493
Python Verification Code Recognition processing instance