Python implements the website registration verification code generation class, and python website registration

Source: Internet
Author: User

Python implements the website registration verification code generation class, and python website registration

This example shares the code of the Python website registration verification code generation class for your reference. The details are as follows:

#-*-Coding: UTF-8-*-''' Created on August 1 @ author: water''' import osimport randomimport stringimport sysimport mathfrom PIL import Image, ImageDraw, ImageFont, ImageFilterfrom django. conf import settings # font position. different versions of the system have different font_path = OS. path. join ('/home/workspace/aofeiKart/static', 'fonts/monaco. ttf') # settings. STATIC_ROOT, 'fonts/MONACO. ttf') font_path = OS. path. join (settings. STATIC_ROOT, 'fonts/mon Aco. ttf') # print font_path # generate the number of digits of the verification code number = 4 # generate the height and width of the Verification Code image size = (255,255,255) # background color, the default is white bgcolor =) # fontcolor. The default value is blue fontcolor = (255,) # 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 # source = list (string. ascii_lowercase + '000000') source = list ('000000') def gene_text (): # return '000000' 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 in the string if draw_line: gene_line (draw, width, height) 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. The boundary is enhanced by image_file = text='.png 'image_path = OS. path. join (settings. STATIC_ROOT, 'images/% s' % image_file) image. save (image_path) # save the verification code image return 'HTTP: // login.chaozu.net: 8000/static/images/% s' % image_file, textif _ name _ = "_ main _": print gene_code ()

The implementation process is very simple. Note the following points:

1. Install the PIL library and set the font storage directory.

2. If the binary data stream of the image is directly returned, it is as follows:

buf = io.BytesIO() #io.BytesIO() #io.StringIO() use it to fill str objimage.save(buf, 'png')request.session['captcha'] = text.lower() return HttpResponse(buf.getvalue(), 'image/png') # return the image data stream as image/jpeg format, browser will treat it as an image

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.