Python Verification Code Simple identification

Source: Internet
Author: User
Tags image processing library

Because of the demand, so contact with the verification code this piece, originally felt difficult, after learning very simple, but later found himself or too young ...

PIL (python Image Library)

The latest official version of PIL is 1.1.7, supported by Python 2.5, 2.6, 2.7,
PIL Official website: http://www.pythonware.com/products/pil/
does not support Python3, but has the master to recompile it to build Python3 under the installable EXE. This unofficial http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil,
, it has not been updated for many years, almost has been abandoned.

Pillow

Pillow is a derived branch of PIL, but has now evolved into a more dynamic image processing library than PIL itself. Pillow can be said to have replaced the PIL, packaging it into a Python library (Pip can be installed), and support Python2 and Python3, the current version is 3.0.0.

Pillow's GitHub homepage: https://github.com/python-pillow/Pillow
Pillow documentation (corresponding to version v3.0.0):
Https://pillow.readthedocs.org/en/latest/handbook/index.html

Installing it is simplepip install pillow

How to use:

#python2
import Image
#python3 (because it is a derived PIL library, you want to import the image in PiL)
from PIL import Image

Taking Python3 as an example,

    • Open
Imageim = Image.open("1.png")im.show()
    • Format

The Format property defines how the image is formatted, and if the image is not open from a file, the property value is a tuple that represents the width and height (in pixels) of the image, and the mode attribute is the pattern for the image, the commonly used mode is: L is a grayscale, RGB is true color, none;size CMYK is a pre-press image. If the file cannot be opened, a IOError exception is thrown.

print(im.format, im.size, im.mode)
    • Save
im.save("c:\\")
    • Convert ()

Convert () is a method of an image instance object that accepts a mode parameter to specify a color pattern, which can take the following values:
· 1 (1-bit pixels, black and white, stored with one pixel per byte)
· L (8-bit pixels, black and white)
· P (8-bit pixels, mapped to any other mode using a colour palette)
· RGB (3x8-bit pixels, true colour)
· RGBA (4x8-bit pixels, true colour with transparency mask)
· CMYK (4x8-bit pixels, colour separation)
· YCbCr (3x8-bit pixels, colour video format)
· I (32-bit signed integer pixels)
· F (32-bit floating point pixels)

im = Image.open(‘1.png‘).convert(‘L‘)
    • Filter

From PIL import Image, ImageFilter
im = Image.open (' 1.png ')
# Gaussian Blur
Im.filter (Imagefilter.gaussianblur)
# general Blur
Im.filter (Imagefilter.blur)
# Edge Enhancement
Im.filter (imagefilter.edge_enhance)
# Find The Edge
Im.filter (Imagefilter.find_edges)
# embossed
Im.filter (Imagefilter.emboss)
# contour
Im.filter (Imagefilter.contour)
# sharpening
Im.filter (Imagefilter.sharpen)
# smooth
Im.filter (Imagefilter.smooth)
# detail
Im.filter (Imagefilter.detail)

    • View image histogram

      im.histogram()

    • Convert image File format

def img2jpg(imgFile):        if type(imgFile)==str and imgFile.endswith((‘.bmp‘, ‘.gif‘, ‘.png‘)): with Image.open(imgFile) as im: im.convert(‘RGB‘).save(imgFile[:-3]+‘jpg‘) 

 img2jpg ( img2jpg ( ' 1.bmp ') img2jpg ( Span class= "hljs-string" > ' 1.png ')        
    • Screen

      From PIL import Imagegrab
      im = Imagegrab.grab ((0,0,800,200)) #截取屏幕指定区域的图像
      im = Imagegrab.grab () #不带参数表示全屏幕

    • Image cropping and pasting

      box = (194, 294) #定义裁剪区域
      Region = im.crop (box) #裁剪
      Region = Region.transpose (image.rotate_180)
      Im.paste (Region,box) #粘贴

    • Image scaling

      im = Im.resize ((100,100)) #参数表示图像的新尺寸, representing width and height, respectively

    • Image contrast Enhancement

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 ()            
pytesseract

Python-tesseract is a python tool for optical character recognition (OCR) that identifies embedded text from a picture. Python-tesseract is a layer of packaging for Google TESSERACT-OCR. It can also be used as a separate invocation script for the Tesseract engine, supporting a variety of image file types that are read by the PIL library (Python Imaging libraries), including JPEG, PNG, GIF, BMP, TIFF, and other formats. Use it as a script to print out the recognized text instead of writing to a file. So install pytesseract before installing the PIL (that is, now Pillow) and Tesseract-orc these two dependent libraries

TESSERACT-OCR is Google's Open source project for image text recognition, Specific installation use can refer to Picture text OCR recognition-tesseract-ocr4.00.00 installation use (4.000 version is the experimental version, we recommend the use of the official version of 3.X)


pytesseract is a library that calls tesseract , so the tesseractmust be installed first,
How to install Pytesseract

Pip Install Pytesseract

How to use Pytesseract

from PIL import Image 
import pytesseract 
print(pytesseract.image_to_string(Image.open(‘test.png‘))) 
print(pytesseract.image_to_string(Image.open(‘test-european.jpg‘), lang=‘fra‘))
#后面的lang是语言的意思, FRA is the meaning of French.

Very simply, Pytesseract has only one simple image_to_string method.

Example
Image:

from PIL import Imagefrom PIL import ImageEnhanceimport pytesseractim=Image.open("1.jpg")im=im.convert(‘L‘)im.show()im=ImageEnhance.Contrast(im)im=im.enhance(3)im.show()print(pytesseract.image_to_string(im))

Results:
Figure One:
Figure II:

Printing results:

Coding Platform

As we all know, the above verification code identification is pediatrics, but also simple above can go to noise, change grayscale What, if encountered a magical interference line, or a wonderful combination of letters, then basically break the dish, the current verification code identification still has a huge problem, so the code platform is enduring, So many years later, not only did not go to the MO, but got a rapid development, of which a few more famous, the next time to add ...

--To supplement the ———
This choice is the Cloud coding platform (non-advertising), of course, but I chose the cloud code, in fact, other coding platform are similar,
First of all to distinguish between the Chuyun Code platform account type
There are two types of cloud coding platform: Users and Developers
We just have to use the user .

After the registration can choose to recharge, first charge 1 yuan, there are 2000 points, the following is the use of points description:

That means 1 bucks can probably be used more than 50 times, which is acceptable.

It's a lot easier next.
User username: ...
Password Password: .....
Account balance Balance: ... min..

just modify the verification code address and the type of verification code, fill in the registration of the user name and password , and then run to output relevant information, visual Sina Weibo verification code recognition rate of more than 95%

Directly run the following source code (remember to change the above requirements)

Import Http.client, Mimetypes, Urllib, JSON, time, requests#######################################################################验证码地址 * * (remember to modify) **image="C:\\users\\desktop\\orc\\1.png"#验证码种类 * * (remember to modify) **species=1005ClassYdmhttp:apiurl =' http://api.yundama.com/api.php ' username ="' Password ="' AppID ="' Appkey =‘‘Def__init__(Self, username, password, AppID, appkey): Self.username = Username Self.password = password Self.appid = str (appid) self. Appkey = AppkeyDefRequest(Self, fields, files=[]): Response = Self.post_url (Self.apiurl, fields, files) response = Json.loads (response)return responseDefBalance(self): data = {' Method ':' Balance ',' username ': self.username,' Password ': Self.password,' AppID ': self.appid,' Appkey ': self.appkey} response = self.request (data)if (response):if (response[' RET ']and response[' RET '] <0):Return response[' RET ']ElseReturn response[' Balance ']ElseReturn-9001DefLogin(self): data = {' Method ':' Login ',' username ': self.username,' Password ': Self.password,' AppID ': self.appid,' Appkey ': self.appkey} response = self.request (data)if (response):if (response[' RET ']and response[' RET '] <0):Return response[' RET ']ElseReturn response[' UID ']ElseReturn-9001DefUpload(self, filename, CodeType, timeout): data = {' Method ':' Upload ',' username ': self.username,' Password ': Self.password,' AppID ': self.appid,' Appkey ': Self.appkey,' CodeType ': Str (codetype),' Timeout ': str (timeout)} file = {' File ': filename} response = self.request (data, file)if (response):if (response[' RET ']and response[' RET '] <0):Return response[' RET ']ElseReturn response[' CID ']ElseReturn-9001DefResult(Self, CID): data = {' Method ':' Result ',' username ': self.username,' Password ': Self.password,' AppID ': self.appid,' Appkey ': Self.appkey,' CID ': str (CID)} response = Self.request (data)return responseand response[' Text ']Or‘‘DefDecode(self, filename, CodeType, timeout): CID = self.upload (filename, codetype, timeout)if (CID >0):For IIn range (0, timeout): result = Self.result (CID)if (Result! =‘‘):Return CID, resultElse:time.sleep (1)Return-3003,‘‘ElseReturn CID,‘‘DefPost_url(Self, URL, fields, files=[]):For keyIn Files:files[key] = open (Files[key],' RB '); res = requests.post (URL, files=files, data=fields)Return Res.text####################################################################### username (fill in your own) * * (remember to modify) **username =‘*************‘# password (fill your own) * * (remember to modify) **password =‘*******‘# Software ID, the developer is divided into necessary parameters. Login to the developer backend "my software" to get! (non-developer without tube) AppID =1# Software key, the developer is divided into necessary parameters. Login to the developer backend "my software" to get! (non-developer without tube) Appkey =' 22cc5376925e9387a23cf797cb9ba745 '# image File filename = image# Verification code Type, # example: 1004 means 4-digit alphanumeric, different types charge different. Please fill in accurately, otherwise affect the recognition rate. All types in this query Http://www.yundama.com/price.htmlcodetype = species# timeout, seconds timeout =  $ # Check if (username = = ' username '): print (' please set the relevant parameters again test ')else: # Initialize Yund AMA = ydmhttp (username, password, appid, Appkey) # Landing Cloud Code UID = Yundama.login (); Print (' uid:%s '% uid) # query Balance Balance = Yundama.balance (); Print (' balance:%s '% balance) # start recognition, picture path, CAPTCHA type ID, time-out (seconds), recognition result cid, result = Yundama.decode (filename, CodeType, timeout); Print (' cid:%s, result:%s '% (CID, result))############################################################## ########

Several verification codes for Sina Weibo are included:

Summary

Verification code is a magical product, it fully contained a large number of computer network impulse, is such a small verification code blocked a mighty army, but I believe that the future algorithm will certainly be the product of the solution out of-. -。 -。 -。 -。 -。 -。

Python Verification Code Simple identification

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.