Selenium identification Login Verification code---Python-based implementation

Source: Internet
Author: User

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]
  1. #-*-Coding:utf-8-*-
  2. From PIL import Image
  3. From PIL import imageenhance
  4. #原始图像
  5. Image = Image.open (' lena.jpg ')
  6. Image.show ()
  7. #亮度增强
  8. Enh_bri = imageenhance.brightness (image)
  9. Brightness = 1.5
  10. image_brightened = enh_bri.enhance (brightness)
  11. Image_brightened.show ()
  12. #色度增强
  13. Enh_col = Imageenhance.color (image)
  14. color = 1.5
  15. image_colored = enh_col.enhance (color)
  16. Image_colored.show ()
  17. #对比度增强
  18. Enh_con = imageenhance.contrast (image)
  19. contrast = 1.5
  20. image_contrasted = enh_con.enhance (contrast)
  21. Image_contrasted.show ()
  22. #锐度增强
  23. Enh_sha = imageenhance.sharpness (image)
  24. Sharpness = 3.0
  25. image_sharped = enh_sha.enhance (sharpness)
  26. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.