Use python to generate a verification code Image

Source: Internet
Author: User

Use python to generate a verification code Image
Introduction

Basically, you will encounter verification codes when using each network service. It is generally a verification method set by a website to prevent malicious registration and posting. The generation principle is to generate an image with a random number or symbol, and add some interference pixels to the image to prevent OCR ). The following describes how to generate a verification code.

Required Environment

In addition to the configured python environment, you also need to have the PIL library in python. This is a python library used to process images. If you use the traditional pip install method or download the python setup. py install method to install the library, an error may be reported (depending on the running environment ). You can use the following method:

1. Download the installation package URL: http://www.pythonware.com/products/pil/index.htm. download all platforms. 2. decompress: tar-zxv-f Imaging-1.1.7.tar.gz3. go to the decompressed Directory: cd Imaging-1.1.74.Bulid pakage: python setup. py build_ext-i5. test: python selftest. py6. installation: python setup. py install
Code Implementation

To generate a verification code image, we must first generate a random string containing 26 letters and 10 numbers.

# Used to randomly generate a string def gene_text (): source = list (string. letters) for index in range (0, 10): source. append (str (index) return ''. join (random. sample (source, number) # number is the number of digits that generate the verification code

Then we need to create an image and write the string. The font here depends on different systems. If the system font path is not found, we can leave it empty.

Def gene_code (): width, height = size # width and height image = Image. new ('rgba', (width, height), bgcolor) # create an image font = ImageFont. truetype (font_path, 25) # font and font size of the Verification Code draw = ImageDraw. draw (image) # create paint brush text = gene_text () # generate the string font_width, font_height = font. getsize (text) draw. text (width-font_width)/number, (height-font_height)/number), text, \ font = font, fill = fontcolor) # fill the string

Next, we will draw several interference lines on the image.

# Used to draw interference line def gene_line (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 = linecolor)

Create a twist and add a filter to enhance the verification code.

Image = image. transform (width + 20, height + 10), Image. AFFINE, (1,-0.3, 0,-0.1,), Image. BILINEAR) # create a distorted image = image. filter (ImageFilter. EDGE_ENHANCE_MORE) # filter, boundary enhancement image.save('idencode.png ') # Save the verification code Image

The following is a verification code generated using the above program.

The complete code is as follows:

# Coding = utf-8import randomimport stringimport sysimport mathfrom PIL import Image, ImageDraw, ImageFont, ImageFilter # font location, different versions of the system will have different font_path = '/Library/Fonts/Arial. ttf' # Generate several digits of the verification code number = 4 # generate the verification code image height and width size = (255,255,255) # background color, the default is white bgcolor = () # font color, the default value is blue fontcolor = (255,) # The interference line color. The default value is red linecolor = (, 0) # whether to add the interference line draw_line = True # the upper and lower limit of the number of interference lines =) # used to randomly generate a string def gene_text (): source = list (string. letters) for index in range (0, 10): source. append (str (index) return ''. join (random. sample (source, number) # number is the number of digits that generate the verification code # used to draw the interference line def gene_line (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 = linecolor) # generate the verification code def gene_code (): width, height = size # width and height image = Image. new ('rgba', (width, height), bgcolor) # create an image font = ImageFont. truetype (font_path, 25) # The font of the Verification Code, draw = ImageDraw. draw (image) # create paint brush text = gene_text () # generate the string font_width, font_height = font. getsize (text) draw. text (width-font_width)/number, (height-font_height)/number), text, \ font = font, fill = fontcolor) # fill the string if draw_line: gene_line (draw, width, height) # image = image. transform (width + 30, height + 10), Image. AFFINE, (1,-0.3, 0,-0.1,), Image. BILINEAR) # create a distorted image = image. transform (width + 20, height + 10), Image. AFFINE, (1,-0.3, 0,-0.1,), Image. BILINEAR) # create a distorted image = image. filter (ImageFilter. EDGE_ENHANCE_MORE) # filter, boundary enhancement image.save('idencode.png ') # Save the verification code image if _ name _ = "_ main _": gene_code ()

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.