使用Pillow生成简单的验证码

本想做成字体各自按随机角度倾斜, 但没有在Pillow中找到相关的方法

import random
from PIL import Image, ImageDraw, ImageFont
import time
import os
import string class GetCaptcha:
size = (100, 40)
source = tuple(string.digits + string.ascii_letters)
number = 4
background_color = (250, 250, 250)
fontsize = 25
font_path = os.path.join(os.path.dirname(__file__), 'verdana.ttf')
random.seed(time.time())
fontcolor = (random.randint(0, 80), random.randint(0, 80), random.randint(0, 80))
draw_line = True
line_number = 3
line_color = (random.randint(0, 250), random.randint(0, 255), random.randint(0, 250))
draw_point = True
rotate = True @classmethod
def image_rotate(cls, original_image):
image = original_image.rotate(random.randint(-15, 15), expand=0)
back_ground = Image.new('RGBA', cls.size,cls.background_color)
return Image.composite(image, back_ground, image) @classmethod
def draw_text(cls):
"""return a captcha include digit and letter"""
return ''.join(random.sample(cls.source, cls.number)) @classmethod
def __draw_line(cls, 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=cls.line_color) @classmethod
def __draw_point(cls, draw, point_size, width, height):
size = min(100, max(0, int(point_size)))
for w in range(width):
for h in range(height):
tmp = random.randint(0, 100)
if tmp > 100 - size: # make sure would not point too much
draw.point((w, h), fill=(150, 150, 150)) @classmethod
def draw_captcha(cls):
width, height = cls.size
image = Image.new('RGBA', (width, height), cls.background_color)
font = ImageFont.truetype(cls.font_path, cls.fontsize)
draw = ImageDraw.Draw(image)
text = cls.draw_text()
font_width, font_height = font.getsize(text)
draw.text(((width - font_width) / 2, (height - font_height) / 2), text, font=font, fill=cls.fontcolor) if cls.draw_line:
for x in range(0, cls.line_number):
cls.__draw_line(draw, width, height) if cls.draw_point:
cls.__draw_point(draw, 5, width, height) return (text, cls.image_rotate(image)) if __name__ == "__main__":
captcha = GetCaptcha()
text, image = captcha.draw_captcha()
image.save('code.png', "PNG")

在Django的视图中返回

def captcha_view(request):
text, image = GetCaptcha.draw_captcha()
output = BytesIO()
image.save(output,'png')
output.seek(0)
response = HttpResponse()
response['content/type'] = "image/png"
response.write(output.read())
return response

使用Pillow库 创建简单验证码的更多相关文章

  1. php笔记之GD库图片创建/简单验证码

    燕十八 公益PHP培训 课堂地址:YY频道88354001 学习社区:www.zixue.it php画图:比如说验证码,缩略图,加水印都要用到GD库,所以要开启gd2库,才能用 首先找到php.in ...

  2. php中运用GD库实现简单验证码

    昨天学习了运用php的GD库进行验证码的实现. 首先可以用phpinfo()函数看一下GD库有没有安装,我用的wampserver是自动给安装的. 主要的步骤是: 1.生成验证码图片 2.随机生成字符 ...

  3. PHP 用session与gd库实现简单验证码生成与验证的类

    验证码是为了防止机器灌水给网站带来污染以及增加服务器负担而出现的.目前大大小小的网站都有验证码.今天自己实现了一个简单的验证码类.说简单是因为没有加一些干扰的弧线等等,只是将文字旋转了一下.当然,因为 ...

  4. 基于Python Pillow库生成随机验证码

    from PIL import Image from PIL import ImageDraw from PIL import ImageFont import random class ValidC ...

  5. python基于pillow库的简单图像处理

    from PIL import Image from PIL import ImageFilter from PIL import ImageEnhance import matplotlib.pyp ...

  6. php中GD库的简单使用

    在php中需要图像处理的地方GD库会发挥重要的作用,php可以创建并处理包括GIF,PNG,JPEG,WBMP以及XPM在内的多种图像格式,简单的举几个例子: 1.用GD库会创建一块空白图片,然后绘制 ...

  7. 用imagemagick和tesseract-ocr破解简单验证码

    用imagemagick和tesseract-ocr破解简单验证码 Tesseract-ocr据说辨识程度是世界排名第三,可谓神器啊. 准备工作: 1.安装tesseract-ocr sudo apt ...

  8. 基于TensorFlow的简单验证码识别

    TensorFlow 可以用来实现验证码识别的过程,这里识别的验证码是图形验证码,首先用标注好的数据来训练一个模型,然后再用模型来实现这个验证码的识别. 生成验证码 首先生成验证码,这里使用 Pyth ...

  9. php生成动态验证码 加减算法验证码 简单验证码

    预览效果: <?php /** *ImageCode 生成包含验证码的GIF图片的函数 *@param $string 字符串 *@param $width 宽度 *@param $height ...

随机推荐

  1. yii DbCriteria相关属性常用方法

    $criteria = new CDbCriteria; //函数方式 $criteria->addCondition("id=1"); //查询条件,即where id = ...

  2. 【编程语言】extern "C"让C++与C进行混合编程

    最近工作不算轻松,现在作为一个项目的负责人统一管理着前端.后端.设计.产品,身上肩负着不小责任,虽然有压力但是对于自己也是一种锻炼.同时自己也在负责整个后端的架构设计,虽然后端经验不是很多,但是自己正 ...

  3. Java /C# 实现文件压缩

    纯粹为了记录. 参考了 https://www.cnblogs.com/zeng1994/p/7862288.html import java.util.List; import java.util. ...

  4. requests库详解 --Python3

    本文介绍了requests库的基本使用,希望对大家有所帮助. requests库官方文档:https://2.python-requests.org/en/master/ 一.请求: 1.GET请求 ...

  5. CentOS7.5下安装、配置MySql数据库 --CentOS7.5

    1.下载MySql的rpm包 [root@VM_39_157_centos -]# wget http://repo.mysql.com/mysql-community-release-el7-5.n ...

  6. Elinks介绍

    Elinks是基于文本的免费浏览器,用于Unix及基于Unix的系统.Elinks支持 HTTP,HTTP Cookies以及支持浏览Perl和Ruby脚本.也很好的支持选项卡浏览.最棒的是它支持鼠标 ...

  7. 高性能异步Socket框架

    Server: using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; ...

  8. 微信小程序1rpx border ios真机显示不全问题

    无意间测试发现,把border的颜色的透明度颜色改成0.99就可以了.1就不行. 边框显示不全的写法: border:1rpx solid rgba(244,84,80,1); 将边框代码的透明度改成 ...

  9. 控制dom 加载成功后事件

  10. wget 使用

    1.很多软件官网会有安装脚本,并把脚本搞成raw模式,方便下载后直接运行的shell文件.比如docker wget -qO- get.docker.com | bash -q的含义是:--quiet ...