Home  >  Article  >  Backend Development  >  Introduction to the method of generating verification code images in python3 pillow

Introduction to the method of generating verification code images in python3 pillow

巴扎黑
巴扎黑Original
2017-09-20 09:55:102053browse

This article mainly introduces the example of python3 pillow to generate a simple verification code image, which is of great practical value. Friends in need can refer to it

Use Python's pillow module random module to randomly generate verification code images, and Apply to Django project

Install pillow


$ pip3 install pillow

Generate verification code image


\vericode.py

from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random

 #随机码 默认长度=1
def random_code(lenght=1):  
  code = ''
  for char in range(lenght):
    code += chr(random.randint(65,90))
  return code

 #随机颜色 默认颜色范围【1,255】
def random_color(s=1,e=255):
  return (random.randint(s,e),random.randint(s,e),random.randint(s,e))

 #生成验证码图片
 #length 验证码长度
 #width 图片宽度
 #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',32)
  #创建Draw对象
  draw = ImageDraw.Draw(image)
  #随机颜色填充每个像素
  for x in range(width):
    for y in range(height):
      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 the view function under Django application


\views.py

from . import vericode.py
from io import BytesIO
from django.http import HttpResponse

def verify_code(request):
  f = BytesIO()
  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')

Django session is used here, which needs to be in Django settings. Add 'django.contrib.sessions' (added by default) to INSTALLED_APPS in py
verify_code view function adds the verification code to the session and sends the verification code image to the browser. When submitting the form to submit_xxx(), first Obtain the verification code from the session and compare it with the verification code entered from the form.

This is just a brief explanation, the url configuration and front-end code are not given.

The above is the detailed content of Introduction to the method of generating verification code images in python3 pillow. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn