Random authentication code generation and acquisition--based on Python tkinter, pytesseract implementation

Source: Internet
Author: User

Drop ~ Today.

Before touching a bit of tkinter, GUI programming a little interest, so on their own thinking to write a small program (haha, too small can not be small program). Well, after all, it's all about the automated tests, so it's a matter of having an automated test. In last week's blogger's notes, there was a solution to the validation code encountered when writing an automated logon script.

Before we mentioned four solutions, let's review:

(1) Remove the verification code: the Verification code code comments out

(2) Set the universal code

(3) Verification Code recognition technology (through Python-tesseract recognition Picture Verification code)

(4) record the cookie, bypassing the CAPTCHA (before the user logs in, the username password is written to the browser cookie via the Add_cookie () method, and the system login connection is automatically logged in)

Of these, the 3rd method has not been in detail before, today is the use of Tesserach to identify the small program generated by the random verification code.

OK, the code journey begins.

1. Create and initialize a window

#Tk () is a class that creates Windows
root = Tk ()
#设置窗体大小
Root.geometry ("400x300")
#把root这个顶层窗体作为一个对象传入我们定义的window类
App = Window (root)

2. Bind some menus and buttons to the form and add a command to the button for the transaction

Edit=menu (Menu)
Edit.add_command (label= ' Get Verification Code ', Command=self.readidentifyingcode)
Edit.add_command (label=u ' Generate English Verification code ', Command=self.showidentifying_english)
Menu.add_cascade (label= ' Edit ', Menu=edit)

3. Write button response transaction. Here are two, one is randomly generated verification code, one is scan get the verification code just generated

(1) Generate random verification code

The main idea is to use the Random.randint () function to randomly generate characters, background color and text color. Then use Imagedraw.draw to fill the dots. Create an image after the image to do the fuzzy processing, you get a verification code map

Write three methods to randomly generate characters, background colors, and font color:

Return Chr (Random.randint (65,90)) #生成随机字符

Return (Random.randint (64,255), Random.randint (64,255), Random.randint (64,255)) #生成随机背景色/Font Color

Fill background and generate random 4-bit validation code

Draw=imagedraw.draw (image)
For x in range (width):
For y in range (heigth):
Draw.point ((x,y), Fill=window.rndcolor ())
For T in range (4):
Draw.text ((60*t+10,10), Window.rndchar (), Font=font,fill=window.rndcolortxt ())

Finally, make a blur of the generated picture

Image=image.filter (Imagefilter.blur)
OK, run the script, get the effect diagram as follows

Next, get the verification code. The basic idea is to get the captcha image, then do a series of operations such as two value, enhance contrast, and then call the Pytesseract.image_to_string () method to get

Image=image.open (U ' e:\ automated test script \code.jpg ')
Image1=image.convert (' L ') #图像加强, binary
Image2=imageenhance.contrast (Iamge1) #增强对比度
Image2=sharpness.enhance (2.0)

Code=pytesseract.image_to_string (Image2)

So we can get the random captcha code we just generated.

Related Article

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.