This paper is mainly to realize verification code recognition by PIL+PYTESSERACT+TESSERACT-OCR
Where PiL is the Python Imaging library, it is already the Python platform de facto image processing standard library. The PIL feature is very powerful, but the API is very easy to use.
PIL third-party libraries install PIP install PIL
The image class is a very important class in the PIL library, which can be used to create an instance with three methods, such as loading image files directly, reading processed images, and images obtained by fetching.
Common image operations:
import Image # 打开一个jpg图像文件
im = Image.open(‘/Users/michael/test.jpg‘)
# 获得图像尺寸: w, h = im.size
# 把缩放后的图像用jpeg格式保存: im.save(‘/Users/michael/thumbnail.jpg‘, ‘jpeg‘)
Image enhancement (PiL Library Imageenhance Class)
In Python, there is a class called Imageenhance in the PiL module, which is specifically designed for image enhancement, which not only enhances (or weakens) the brightness, contrast, chroma of the image, but also enhances the sharpness of the image.
See the following example:
[Python]
- #-*-Coding:utf-8-*-
- From PIL import Image
- From PIL import imageenhance
- #原始图像
- Image = Image.open (' lena.jpg ')
- Image.show ()
- #亮度增强
- Enh_bri = imageenhance.brightness (image)
- Brightness = 1.5
- image_brightened = enh_bri.enhance (brightness)
- Image_brightened.show ()
- #色度增强
- Enh_col = Imageenhance.color (image)
- color = 1.5
- image_colored = enh_col.enhance (color)
- Image_colored.show ()
- #对比度增强
- Enh_con = imageenhance.contrast (image)
- contrast = 1.5
- image_contrasted = enh_con.enhance (contrast)
- Image_contrasted.show ()
- #锐度增强
- Enh_sha = imageenhance.sharpness (image)
- Sharpness = 3.0
- image_sharped = enh_sha.enhance (sharpness)
- Image_sharped.show ()
image enhancement to better identify more complex verification codes
tesseract: Open source OCR recognition engine, the initial tesseract engine was developed by HP Labs, later contributed to the open source software industry, and then improved by Google, eliminating bugs, optimizing, republishing. Current version is 3.02
Tesseract-ocr:http://jaist.dl.sourceforge.net/project/tesseract-ocr-alt/tesseract-ocr-setup-3.02.02.exe
After download, install, by default, the installer will give you to configure the system environment variables to point to the installation directory (then you can run tesseract in any directory through the DOS interface). After the installation is complete, the directory is as follows:
Appendix:
The Tessdata directory contains the language font files, and the files that correspond to the parameters that may be used in the command line interface. This installer contains the English font by default.
If you want to be able to identify Chinese, you can download the corresponding language font file to http://code.google.com/p/tesseract-ocr/downloads/list. General Google cannot access, please download here,
Simplified Chinese font file is: http://download.csdn.net/detail/wanghui2008123/7621567 after the download is complete, and then cut the file to the Tessdata directory to go down.
See also: http://www.cnblogs.com/wzben/p/5930538.html
tesseract is not intended to be used directly in Python, it needs to use Python's wrapper class Pytesseract
Python-tesseract is the Python wrapper class for optical character recognition Tesseract OCR engines. Ability to read any regular picture file (JPG, GIF, PNG, TIFF, etc.) and decode it into a readable language. No pro files are created during OCR processing
The following steps are summed up to identify:
1. Install PIL 2. Installing tesseract 3. Installing the Pytesseract
Here's an example to speak!
#Coding:utf-8 fromSeleniumImportWebdriver fromTimeImportSleepImportUnitTest fromPILImportImage fromPILImportimageenhanceImportPytesseractdriver=Webdriver. Firefox () URL="Https://passport.baidu.com/?getpassindex"driver.get (URL) driver.maximize_window () Driver.save_screenshot (R"E:\aa.png")#intercept the current page, which has the verification code we needImgElement = Driver.find_element_by_xpath (".//*[@id = ' Forgotsel ']/div/div[3]/img")#imgelement = driver.find_element_by_id ("code") #定位验证码Location = Imgelement.location#Get verification code x, y axis coordinatesSize=imgelement.size#get the length and width of the verification codecoderange= (int (location['x']), int (location['y']), int (location['x']+size['width']), int (location['y']+size['Height']))#as the position coordinates we need to intercept.I=image.open (R"E:\aa.png")#OpenFrame4=i.crop (Coderange)#use the crop function of image to intercept the area we need againFrame4.save (R"E:\frame4.png") I2=image.open (R"E:\frame4.png") Imgry= I2.convert ('L')#image enhancement, binary, there are nine different modes in PIL. The 1,l,p,rgb,rgba,cmyk,ycbcr,i,f are respectively. L is a grayscale imageSharpness =imageenhance.contrast (Imgry)#Contrast Enhancementi3 = sharpness.enhance (3.0)#3.0 saturation of the imageI3.save ("E:\\image_code.png") I4=image.open ("E:\\image_code.png") Text=pytesseract.image_to_string (I2). Strip ()#using image_to_string to identify verification codesPrintText
Selenium identification Login Verification code---Python-based implementation