I. Preparation and code examples
(1) Installation pil: After the download is an EXE, directly double-click the installation, it will automatically install into the C:\Python27\Lib\site-packages,
(2) Pytesser: Download after decompression directly put C:\Python27\Lib\site-packages (depending on the Python path you installed different), at the same time, a new pytheeer.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: Extract after download, Tessdata folder, use it to replace the Pytesser extracted Tessdata folder.
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:
From pytesser import * image = Image.open (' 1.jpg ') # Open Image object using PIL print image_to_string (image)
Then run:
or directly:
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
# Verification Code identification, this program can only recognize data verification code import Image import imageenhance import imagefilter import sys from pytesser import * # binary threshold = Table = [] for i in range: if I < threshold: table.append (0) else: table.append (1) #由于都 is the number #对于识别成字母的 using the table to fix 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) #二值化, using the threshold segmentation method, threshold is the split 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
Post-run Effects:
The above is the whole content of this article, I hope that everyone's study has helped.