Python3 Pillow Generate Verification Code picture method Introduction

Source: Internet
Author: User
This article mainly introduces the Python3 pillow to generate a simple verification code picture example, very practical value, the need for friends can refer to the following

Randomly generate a captcha picture using the Python Pillow Module random module and apply it to the Django project

Installing Pillow


$ PIP3 Install Pillow

Generate a Captcha picture


\vericode.pyfrom PIL Import image,imagedraw,imagefont,imagefilterimport random #随机码 default length =1def random_code (lenght=1):    code = "for  char in range (lenght):    code + = Chr (Random.randint (65,90))  return code #随机颜色 default color range" 1,255 " def random_color (s=1,e=255):  return (Random.randint (s,e), Random.randint (S,e), Random.randint (s,e)) #生成验证码图片 # Length captcha #width picture width #height picture height #返回验证码和图片def veri_code (lenght=4,width=160,height=40):  #创建Image对象  image = Image.new (' RGB ', (width,height), (255,255,255))  #创建Font对象  font = imagefont.truetype (' Arial.ttf ', +)  # Create Draw object  draw = Imagedraw.draw (image)  #随机颜色填充每个像素 for  x in range (width): A for    y in range (height): C14/>draw.point ((x, y), Fill=random_color (64,255))  #验证码  code = Random_code (lenght)  #随机颜色验证码写到图片上  for T in range (lenght):    draw.text ((40*t+5,5), Code[t],font=font,fill=random_color (32,127))  #模糊滤镜  image = Image.filter (Imagefilter.blur)  return code,image

Application

Write a view function under a Django application


\views.pyfrom. Import vericode.pyfrom IO import bytesiofrom django.http import httpresponsedef verify_code (Request):  f = Bytesio () C1/>code,image = Vericode.veri_code ()  image.save (f, ' jpeg ')  request.session[' vericode '] = code  return HttpResponse (F.getvalue ()) def submit_xxx (request):  if Request.method = = "POST":    Vericode = Request.session.get ("Vericode"). Upper ()    Submitcode = Request. Post.get ("Vericode"). Upper ()    if Submitcode = = Vericode:      return HttpResponse (' OK ')  return HttpResponse ( ' ERROR ')

The Django session is used here to add ' django.contrib.sessions ' to the Installed_apps of the Django settings.py (added by default)
The Verify_code view function adds the verification code to the session and the CAPTCHA image to the browser, and when the form is submitted to submit_xxx (), the verification code is obtained from the session, and then the verification code entered from the form is compared.

Here is simply a description of the URL configuration and the front-end code is not given.

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.