Python image processing production 4-digit verification Code

Source: Internet
Author: User
Tags image processing library

Image processing is a very wide-spread technology, and Python, which has a very rich third-party expansion library, will certainly not miss this feast. PIL (Python Imaging library) is the most commonly used image processing library in Python, and if you are python2.x, you can download it at the following address: http://www.pythonware.com/products/pil/ Index.htm, to find the corresponding version to download it.
Note: The PIL module has been replaced in the python3.x Pillow module, the document address: http://pillow.readthedocs.io/en/latest/, directly using PIP3 Install pillow can be installed module, Use the From PIL import Image when importing.

1 production of 4-digit verification codes using the PIL module

Code:

#!/usr/bin/env python #coding: Utf8import randomimportstringImport Sysimport Math fromPIL Import Image, Imagedraw, Imagefont, ImageFilterclassVerificationcode (): Def __init__ (self): # font location, different versions of the system will have different Self.font_path='Msyh.ttf'# Generate several number of verification codes self.number=4# Generate the height and width of the captcha picture self.size= ( -, -) # background color, default is white Self.bgcolor= (255,255,255# font color, default to blue Self.fontcolor= (0,0,255) # interference line Color. The default is red Self.linecolor= (255,0,0) # Do you want to add the interference line Self.draw_line=True # Add the upper and lower bounds of the number of interfering lines Self.line_number= -#验证码内容 Self.text=""# used to randomly generate a string def gene_text (self): source= List (string. Ascii_letters) forIndexinchRange0,Ten): Source.append (str (index)) Self.text="". Join (source, Self.number) # # is the number of bits that generate the Captcha # used to draw the Random.sample line def gene_line (self,draw, width, height): Begin= (Random.randint (0, width), random.randint (0, height)) End= (Random.randint (0, width), random.randint (0, height)) Draw.line ([begin, end], fill=self.linecolor) # Generate verification Code def gene_code (self): width, height=self.size # width and height image= Image.New('RGBA', (width, height), self.bgcolor) # Create a picture font= Imagefont.truetype (Self.font_path, -# code Font draw=Imagedraw.draw (image) # Create brush # text=Self.gene_text () # Generate string print (Self.text) font_width, Font_height=font.getsize (Self.text) Draw.text (((Width-font_width)/Self.number, (height-font_height)/Self.number), Self.text, Font=font, fill=self.fontcolor) # Fill StringifSelf.draw_line: forIinchRange (Self.line_number): Self.gene_line (Draw, width, height) # image= Image.transform ((width + -, Height +Ten), Image.affine, (1, -0.3,0, -0.1,1,0), Image.bilinear) # Create a distorted Image=Image.filter (Imagefilter.edge_enhance_more) # Filter, Border enhancement Image.Save ("{0}.png". Format (Self.text),"PNG"# Save captcha Picture # Image.show () def main (): VC=Verificationcode () Vc.gene_text () Vc.gene_code ( )if__name__ = ="__main__": Main ()

Operation Result:

Python image processing production 4-digit verification Code

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.