码迷,mamicode.com
首页 > 其他好文 > 详细

二.图形验证码

时间:2020-01-10 12:52:49      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:include   linear   输出   怎么   actor   字符串   lambda   用户   sample   

# 图形验证码:

- 作用:注册页面
- 实现思路:
  - 生成4位字符串--产生随机数
  - 绘制图片--python的PIL包,但这里我不用它,我用第三包captcha来绘制
  - 响应,--告诉浏览器--指定数据类型为image/png
- pip install Pillow
- 解压拷贝capcha到libs中
- 实现:
  - 配置路由规则
  - 定义视图,调用capcha的方法
- 视图的逻辑:
  - 调用captcha生成图片数据
  - 保存文本到redis中
  - 输出图片数据

1.创建验证应用:

归根结底本质上就是显示一张图片,html中加一个img标签并指定src就行,但是注意它不是一张固定图片,思路:直接去请求一个视图,视图中去画一张图片出来,并在图片中写文字,再把图片保存到response响应对象中就可返回给浏览器.但有个问题,浏览器怎么知道你返回的是一张图片?--mime type数据格式---我们告诉浏览器我给你的二进制是图片imgae/jpg

capcha包中的capcha.py文件解说:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# refer to `https://bitbucket.org/akorn/wheezy.captcha`

import random
import string
import os.path
from io import BytesIO

from PIL import Image  #image相当于一张纸
from PIL import ImageFilter
from PIL.ImageDraw import Draw  #draw相当于一根笔
from PIL.ImageFont import truetype  #truetype是字体


class Bezier:  #封装绘图功能
    def __init__(self):
        self.tsequence = tuple([t / 20.0 for t in range(21)])
        self.beziers = {}

    def pascal_row(self, n):
        """ Returns n-th row of Pascal‘s triangle
        """
        result = [1]
        x, numerator = 1, n
        for denominator in range(1, n // 2 + 1):
            x *= numerator
            x /= denominator
            result.append(x)
            numerator -= 1
        if n & 1 == 0:
            result.extend(reversed(result[:-1]))
        else:
            result.extend(reversed(result))
        return result

    def make_bezier(self, n):
        """ Bezier curves:
            http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Generalization
        """
        try:
            return self.beziers[n]
        except KeyError:
            combinations = self.pascal_row(n - 1)
            result = []
            for t in self.tsequence:
                tpowers = (t ** i for i in range(n))
                upowers = ((1 - t) ** i for i in range(n - 1, -1, -1))
                coefs = [c * a * b for c, a, b in zip(combinations,
                                                      tpowers, upowers)]
                result.append(coefs)
            self.beziers[n] = result
            return result


class Captcha(object):
    def __init__(self):
        self._bezier = Bezier()
        self._dir = os.path.dirname(__file__)
        # self._captcha_path = os.path.join(self._dir, ‘..‘, ‘static‘, ‘captcha‘)

    @staticmethod
    def instance():
        if not hasattr(Captcha, "_instance"):
            Captcha._instance = Captcha()
        return Captcha._instance

    def initialize(self, width=200, height=75, color=None, text=None, fonts=None):
        # self.image = Image.new(‘RGB‘, (width, height), (255, 255, 255))
        self._text = text if text else random.sample(string.ascii_uppercase + string.ascii_uppercase + 3456789, 4)
        self.fonts = fonts if fonts else             [os.path.join(self._dir, fonts, font) for font in [Arial.ttf, Georgia.ttf, actionj.ttf]]
        self.width = width
        self.height = height
        self._color = color if color else self.random_color(0, 200, random.randint(220, 255))

    @staticmethod
    def random_color(start, end, opacity=None):
        red = random.randint(start, end)
        green = random.randint(start, end)
        blue = random.randint(start, end)
        if opacity is None:
            return red, green, blue
        return red, green, blue, opacity

    # draw image

    def background(self, image):
        Draw(image).rectangle([(0, 0), image.size], fill=self.random_color(238, 255))
        return image

    @staticmethod
    def smooth(image):
        return image.filter(ImageFilter.SMOOTH)

    def curve(self, image, width=4, number=6, color=None):
        dx, height = image.size
        dx /= number
        path = [(dx * i, random.randint(0, height))
                for i in range(1, number)]
        bcoefs = self._bezier.make_bezier(number - 1)
        points = []
        for coefs in bcoefs:
            points.append(tuple(sum([coef * p for coef, p in zip(coefs, ps)])
                                for ps in zip(*path)))
        Draw(image).line(points, fill=color if color else self._color, width=width)
        return image

    def noise(self, image, number=50, level=2, color=None):
        width, height = image.size
        dx = width / 10
        width -= dx
        dy = height / 10
        height -= dy
        draw = Draw(image)
        for i in range(number):
            x = int(random.uniform(dx, width))
            y = int(random.uniform(dy, height))
            draw.line(((x, y), (x + level, y)), fill=color if color else self._color, width=level)
        return image

    def text(self, image, fonts, font_sizes=None, drawings=None, squeeze_factor=0.75, color=None):
        color = color if color else self._color
        fonts = tuple([truetype(name, size)
                       for name in fonts
                       for size in font_sizes or (65, 70, 75)])
        draw = Draw(image)
        char_images = []
        for c in self._text:
            font = random.choice(fonts)
            c_width, c_height = draw.textsize(c, font=font)
            char_image = Image.new(RGB, (c_width, c_height), (0, 0, 0))
            char_draw = Draw(char_image)
            char_draw.text((0, 0), c, font=font, fill=color)
            char_image = char_image.crop(char_image.getbbox())
            for drawing in drawings:
                d = getattr(self, drawing)
                char_image = d(char_image)
            char_images.append(char_image)
        width, height = image.size
        offset = int((width - sum(int(i.size[0] * squeeze_factor)
                                  for i in char_images[:-1]) -
                      char_images[-1].size[0]) / 2)
        for char_image in char_images:
            c_width, c_height = char_image.size
            mask = char_image.convert(L).point(lambda i: i * 1.97)
            image.paste(char_image,
                        (offset, int((height - c_height) / 2)),
                        mask)
            offset += int(c_width * squeeze_factor)
        return image

    # draw text
    @staticmethod
    def warp(image, dx_factor=0.27, dy_factor=0.21):
        width, height = image.size
        dx = width * dx_factor
        dy = height * dy_factor
        x1 = int(random.uniform(-dx, dx))
        y1 = int(random.uniform(-dy, dy))
        x2 = int(random.uniform(-dx, dx))
        y2 = int(random.uniform(-dy, dy))
        image2 = Image.new(RGB,
                           (width + abs(x1) + abs(x2),
                            height + abs(y1) + abs(y2)))
        image2.paste(image, (abs(x1), abs(y1)))
        width2, height2 = image2.size
        return image2.transform(
            (width, height), Image.QUAD,
            (x1, y1,
             -x1, height2 - y2,
             width2 + x2, height2 + y2,
             width2 - x2, -y1))

    @staticmethod
    def offset(image, dx_factor=0.1, dy_factor=0.2):
        width, height = image.size
        dx = int(random.random() * width * dx_factor)
        dy = int(random.random() * height * dy_factor)
        image2 = Image.new(RGB, (width + dx, height + dy))
        image2.paste(image, (dx, dy))
        return image2

    @staticmethod
    def rotate(image, angle=25):
        return image.rotate(
            random.uniform(-angle, angle), Image.BILINEAR, expand=1)

    def captcha(self, path=None, fmt=JPEG):
        """Create a captcha.

        Args:
            path: save path, default None.
            fmt: image format, PNG / JPEG.
        Returns:
            A tuple, (name, text, StringIO.value).
            For example:
                (‘fXZJN4AFxHGoU5mIlcsdOypa‘, ‘JGW9‘, ‘\x89PNG\r\n\x1a\n\x00\x00\x00\r...‘)

        """
        image = Image.new(RGB, (self.width, self.height), (255, 255, 255))
        image = self.background(image)
        image = self.text(image, self.fonts, drawings=[warp, rotate, offset])
        image = self.curve(image)
        image = self.noise(image)
        image = self.smooth(image)
        name = "".join(random.sample(string.ascii_lowercase + string.ascii_uppercase + 3456789, 24))
        text = "".join(self._text)
        out = BytesIO()
        image.save(out, format=fmt)
        if path:
            image.save(os.path.join(path, name), fmt)
        return name, text, out.getvalue()

    def generate_captcha(self):
        self.initialize()
        return self.captcha("")

#那怎么用这个绘图功能呢?
captcha = Captcha.instance()   #实例化对象

if __name__ == __main__:
    print(captcha.generate_captcha())  #这样调用就能生成验证码数据


‘‘‘
怎么用呢?--它里边有这个__main__函数,所以直接单击这个文件右键---run就可输出如下了

(‘oNFHu6a9MyWvRtDQmTwhCXJY‘,
‘M5JK‘, ====>图片中的字符
图片的二进制数据==》b‘\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.\‘ ",#\x1c\x1c(7),01444\x1f\‘9=82<.342\xff\xdb\x00C\x01\t\t\t\x0c\x0b\x0c\x18\r\r\x182!\x1c!22222222222222222222222222222222222222222222222222\xff\xc0\x00\x11\x08\x00K\x00\xc8\x03\x01"\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07"q\x142\x81\x91\xa1\x08#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&\‘()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xc4\x00\x1f\x01\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x11\x00\x02\x01\x02\x04\x04\x03\x04\x07\x05\x04\x04\x00\x01\x02w\x00\x01\x02\x03\x11\x04\x05!1\x06\x12AQ\x07aq\x13"2\x81\x08\x14B\x91\xa1\xb1\xc1\t#3R\xf0\x15br\xd1\n\x16$4\xe1%\xf1\x17\x18\x19\x1a&\‘()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe2\xe3\xe4\xe5\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91\xa2\xaa\x8e\x00\x03\x15^;\x1f/Q\x92\xef\xed\x12\xb6\xf1\x8f(\xb7\xc8>\x82\xad\xd1@4\x98QE\x14\xc6`x\xa7A\xfe\xd9\xb2\x06%\x1fi\x8f\xee\x13\xdf\xda\xbc\xf1\xb4Mj\xd2C\x1a\xdbN\x0f\xfb\x19\xc1\xafb\xa2\xa2T\xd4\x9d\xcc*a\xe3Q\xf3l\xcf.\xb0\xf0V\xab|\xea\xf7?\xb9\x8c\xe0\x92\xe7$\x8a\xf4-\‘I\xb7\xd2-\x05\xbd\xb8\xe3\xa9c\xd4\x9a\xbfE\x11\x82\x8e\xc3\xa5B\x14\xf6\xdc(\xa2\x8a\xb3`\xa2\x8a(\x00\xa2\x8a(\x00\xa2\x8a(\x03\xff\xd9‘)
‘‘‘

上述文件中,对象给我们创建好了,所以我们直接拿到这个对象调方法就好了,所以我接下来就写个视图,接收客户端的请求,来调类的方法,返回一个数据就完了。

技术图片

 

 

我单独创建一个应用--专负责验证的,因为我这个验证在注册模块用到,但别的模块也会用到.

技术图片

 

 verifycations/urls.py中:

这个url不是随便写的,这个js当中写好的,直接复制用就行

from django.conf.urls import url
from . import views
import uuid

urlpatterns = [
url(‘^image_codes/(?P<uuid>[\w-]+)/$‘, views.ImageCodeView.as_view()),
]

根urls.py中包含:

from django.contrib import admin
from django.conf.urls import url,include

urlpatterns = [
    url(r^admin/, admin.site.urls),
    url(r^, include(users.urls)),
    url(^,include(verifycations.urls)),
]

views.py中:

from django.shortcuts import render
from django.views import View
from meiduo_mall.libs.captcha.captcha import captcha  #导入captcha对象
from django_redis import get_redis_connection  #连接redis
from . import constants
from django import http


class ImageCodeView(View):
    def get(self, request, uuid):
        # 接收
        # 验证
        # 处理:
        # 1.生成图片的文本、数据
        text, code, image = captcha.generate_captcha()

        # 2.保存图片文本,用于后续与用户输入值对比
        redis_cli = get_redis_connection(image_code) #连接缓存中配的image_code缓存
        redis_cli.setex(uuid, constants.IMAGE_CODE_EXPIRES, code)#往redis中写数据,键是uuid唯一标识,过期时间,值是code

        # 响应:输出图片数据
        return http.HttpResponse(image, content_type=image/png)#告诉浏览器这是图片类型

 

去注册:dev.py中追加:

INSTALLED_APPS = [
    .......verifycations.apps.VerifycationsConfig,
]

dev.py中增加图片验证的缓存项:

    "image_code": {  # 图形验证码
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/2",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },

新建verifycations/constants.py过期时间文件:

#图片验证码在redis中的过期时间,单位是秒
IMAGE_CODE_EXPIRES = 60 * 5

二.图形验证码

标签:include   linear   输出   怎么   actor   字符串   lambda   用户   sample   

原文地址:https://www.cnblogs.com/dbslinux/p/12175316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!