使用Pillow库 创建简单验证码
使用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库 创建简单验证码的更多相关文章
- php笔记之GD库图片创建/简单验证码
燕十八 公益PHP培训 课堂地址:YY频道88354001 学习社区:www.zixue.it php画图:比如说验证码,缩略图,加水印都要用到GD库,所以要开启gd2库,才能用 首先找到php.in ...
- php中运用GD库实现简单验证码
昨天学习了运用php的GD库进行验证码的实现. 首先可以用phpinfo()函数看一下GD库有没有安装,我用的wampserver是自动给安装的. 主要的步骤是: 1.生成验证码图片 2.随机生成字符 ...
- PHP 用session与gd库实现简单验证码生成与验证的类
验证码是为了防止机器灌水给网站带来污染以及增加服务器负担而出现的.目前大大小小的网站都有验证码.今天自己实现了一个简单的验证码类.说简单是因为没有加一些干扰的弧线等等,只是将文字旋转了一下.当然,因为 ...
- 基于Python Pillow库生成随机验证码
from PIL import Image from PIL import ImageDraw from PIL import ImageFont import random class ValidC ...
- python基于pillow库的简单图像处理
from PIL import Image from PIL import ImageFilter from PIL import ImageEnhance import matplotlib.pyp ...
- php中GD库的简单使用
在php中需要图像处理的地方GD库会发挥重要的作用,php可以创建并处理包括GIF,PNG,JPEG,WBMP以及XPM在内的多种图像格式,简单的举几个例子: 1.用GD库会创建一块空白图片,然后绘制 ...
- 用imagemagick和tesseract-ocr破解简单验证码
用imagemagick和tesseract-ocr破解简单验证码 Tesseract-ocr据说辨识程度是世界排名第三,可谓神器啊. 准备工作: 1.安装tesseract-ocr sudo apt ...
- 基于TensorFlow的简单验证码识别
TensorFlow 可以用来实现验证码识别的过程,这里识别的验证码是图形验证码,首先用标注好的数据来训练一个模型,然后再用模型来实现这个验证码的识别. 生成验证码 首先生成验证码,这里使用 Pyth ...
- php生成动态验证码 加减算法验证码 简单验证码
预览效果: <?php /** *ImageCode 生成包含验证码的GIF图片的函数 *@param $string 字符串 *@param $width 宽度 *@param $height ...
随机推荐
- JVM内存模型和GC机制
目录 1.JVM内存模型 2.GC 1.JVM内存模型 堆,栈,本地方法栈,方法区,程序计数器 2.GC 新生代收集器:Serial(单线程).ParNew.Parallel Scavenge: 老年 ...
- erlang下lists模块sort(排序)方法源码解析(一)
排序算法一直是各种语言最简单也是最复杂的算法,例如十大经典排序算法(动图演示)里面讲的那样 第一次看lists的sort方法的时候,蒙了,几百行的代码,我心想要这么复杂么(因为C语言的冒泡排序我记得不 ...
- Robot Framework--BuiltIn库4
Catenate :关键字可以连接多个信息. Create List :关键字可以定义列表. get time :获取当前时间. Evaluate :数值运算并得到结果: Should系列关键字是Sh ...
- C++标准模板库(STL)之Vector
在C中,有很多东西需要自己实现.C++提供了标准模板库(Standard Template Libray,STL),其中封装了很多容器,不需要费力去实现它们的细节而直接调用函数来实现功能. 具体容器链 ...
- mysql中用limit 进行分页有两种方式
代码示例:语句1: select * from student limit 9,4 语句2: slect * from student limit 4 offset 9 // 语句1和2均返回表stu ...
- PHP基础入门(五)---PHP面向对象实用基础知识
前言: 今天来和大家介绍一下PHP的面向对象.说到面向对象,我不得不提一下面向过程,因为本人在初学时,常常分不清楚面向对象和面向过程,下面就来给大家介绍一下它们的区别: 面向对象专注于由哪个对象来处理 ...
- leetcode 链表相关
1.给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们 ...
- JS数组映射详解
现在这里占个坑位,免的忘了,需要整理一下最近的内容: 1.数组映射的使用 2.微信分享功能详解 3.jq自己封装 4.HTML的富文本应用
- hdu2132
题目:We once did a lot of recursional problem . I think some of them is easy for you and some if hard ...
- 无服务器架构(Faas/Serverless)
摘要无服务器架构(Faas/Serverless),是软件架构领域的热门话题. AWS,Google Cloud和Azure - 在无服务器上投入了大量资金,已经在看到了大量专门针对Faas/Serv ...