


Example of simple verification code recognition function implemented in Python3
This article mainly introduces the simple verification code recognition function implemented in Python3, involving Python's related operating skills for verification code image recognition and processing. Friends in need can refer to it
The examples in this article describe the simple implementation of Python3 Verification code recognition function. Share it with everyone for your reference, the details are as follows:
This time the requirement is to automatically log in to an organization's website. The verification code is very unique and is very suitable for an introductory demo of verification code recognition. I will post the main code first, including pictures. The comparison uses the edit distance algorithm, and the script uses the pillow library
from PIL import Image import requests import re splitter = re.compile(r'\d{30}') # 分割二值化后的图片 # distance('11110000', '00000000') # 比较两个字符串有多少位不同, 返回不同的位数 def distance(string1, string2): d_str1 = len(string1) d_str2 = len(string2) d_arr = [[0] * d_str2 for i in range(d_str1)] for i in range(d_str1): for j in range(d_str2): if string1[i] == string2[j]: if i == 0 and j == 0: d_arr[i][j] = 0 elif i != 0 and j == 0: d_arr[i][j] = d_arr[i - 1][j] elif i == 0 and j != 0: d_arr[i][j] = d_arr[i][j - 1] else: d_arr[i][j] = d_arr[i - 1][j - 1] else: if i == 0 and j == 0: d_arr[i][j] = 1 elif i != 0 and j == 0: d_arr[i][j] = d_arr[i - 1][j] + 1 elif i == 0 and j != 0: d_arr[i][j] = d_arr[i][j - 1] + 1 else: d_arr[i][j] = min(d_arr[i][j - 1], d_arr[i - 1][j], d_arr[i - 1][j - 1]) + 1 current = max(d_arr[d_str1 - 1][d_str2 - 1], abs(d_str2 - d_str1)) # print("Levenshtein Distance is",current) # print(current) return current # 去除字符串里面连续的1 def no_one(string): n_arr = splitter.findall(string) n_arr = filter(lambda each_str: each_str != '111111111111111111111111111111', n_arr) n_result = '' for n_each in n_arr: n_result += str(n_each) return n_result opener = requests.session() res = opener.get('http://60.211.254.236:8402/Ajax/ValidCodeImg.ashx').content with open('verify.gif', 'wb') as v: v.write(res) img = Image.open('verify.gif') img = img.convert('L') size = img.size # img = img.point(table, '1') img_arr = img.load() # for x in range(size[0]): # for y in range(size[1]): # if img_arr[x, y] > 210: # img_arr[x, y] = 1 # else: # img_arr[x, y] = 0 # img.save('after.gif') inc = 0 str1 = '' str2 = '' str3 = '' cur_str = '' for x in range(size[0]): for y in range(size[1]): if img_arr[x, y] > 210: cur_str += '1' else: cur_str += '0' # print(img_arr[i, j], end='') # cur_str += str(img_arr[x, y]) inc += 1 # if inc % 18 == 0: # print('\n----') # else: # print('') if inc == 18: str1 = cur_str cur_str = '' elif inc == 36: str2 = cur_str cur_str = '' elif inc == 54: str3 = cur_str cur_str = '' str1 = str1[:-60] str2 = str2[:-60] str3 = str3[:-60] str1 = no_one(str1) str2 = no_one(str2) str3 = no_one(str3) str1 = str1.strip('1') str2 = str2.strip('1') str3 = str3.strip('1') # print(str1) # print(str3) with open('./dict/plus') as plus: with open('./dict/minus') as minus: p = plus.read() m = minus.read() is_add = 1 if distance(p, str2) < distance(m, str2) else 0 arr1 = [] arr3 = [] for each in range(1, 10): with open('./dict/{}'.format(each)) as f: ff = f.read() arr1.append([each, distance(ff, str1)]) arr3.append([each, distance(ff, str3)]) arr1 = sorted(arr1, key=lambda item: item[1]) arr3 = sorted(arr3, key=lambda item: item[1]) result = arr1[0][0] + arr3[0][0] if is_add else arr1[0][0] - arr3[0][0] print(result) # login_url = 'http://60.211.254.236:8402/Ajax/Login.ashx?Method=G3_Login' # login_data = { # 'loginname': usn, # 'password': pwd, # 'validcode': result, # # } # opener.get(login_url, login_data)
Related recommendations:
Read txt data files in Python3 How to enter the matrix
The above is the detailed content of Example of simple verification code recognition function implemented in Python3. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

How to implement speech recognition and speech synthesis in C++? Speech recognition and speech synthesis are one of the popular research directions in the field of artificial intelligence today, and they play an important role in many application scenarios. This article will introduce how to use C++ to implement speech recognition and speech synthesis functions based on Baidu AI open platform, and provide relevant code examples. 1. Speech recognition Speech recognition is a technology that converts human speech into text. It is widely used in voice assistants, smart homes, autonomous driving and other fields. The following is the implementation of speech recognition using C++

PHP8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses. Verification and signing are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become even more important when dealing with online transactions and sensitive information because if someone is able to tamper with the data, it could potentially

Indian Financial System Code is the abbreviation. Indian bank branches participating in the electronic funds transfer system are identified by a special 11-character code. The Reserve Bank of India uses this code in internet transactions to transfer funds between banks. IFSC code is divided into two parts. Banks are identified by the first four characters, while branches are identified by the last six characters. NEFT (National Electronic Funds Transfer), RTGS (Real Time Gross Settlement) and IMPS (Immediate Payment Service) are some of the electronic transactions that require IFSC codes. Method Some common ways to validate IFSC codes using regular expressions are: Check if the length is correct. Check the first four characters. Checkthefifthcharacter.Che

With the continuous development of artificial intelligence technology, face detection and recognition technology has become more and more widely used in daily life. Face detection and recognition technologies are widely used in various occasions, such as face access control systems, face payment systems, face search engines, etc. As a widely used programming language, Java can also implement face detection and recognition technology. This article will introduce how to use Java to implement face detection and recognition technology. 1. Face detection technology Face detection technology refers to the technology that detects faces in images or videos. in J

Golang is a high-performance, modern programming language that often involves string processing in daily development. Among them, validating whether the input is in uppercase letters is a common requirement. This article will introduce how to verify whether the input is uppercase letters in Golang. Method 1: Use the unicode package. The unicode package in Golang provides a series of functions to determine the encoding type of characters. For uppercase letters, the corresponding encoding range is 65-90 (decimal), so we can use unicod

In golang, Unicode encoding and rune type are required to verify whether the input is full-width characters. Unicode encoding is a character encoding standard that assigns a unique numeric code point to each character in the character set, which includes full-width characters and half-width characters. The rune type is the type used to represent Unicode characters in golang. The first step is to convert the input into a rune type slice. This can be converted by using golang's []rune type, e.g.
