使用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 ...
随机推荐
- springboot缓存注解——@Cacheable
@Cacheable: 1,方法运行之前,先查询Cache(缓存组件),按照cacheName指定的名字获取(CacheManager获取相应缓存) 第一次获取缓存如果没有Cache组件会自会自动创建 ...
- Venom- Eminem
I got a song filled with shit for the strong willed. 我写了一首充满戾气的歌献给意志坚强的人. When the world give you a ...
- JS回调函数中的this指向(详细)
首先先说下正常的this指向问题 什么是this:自动引用正在调用当前方法的.前的对象. this指向的三种情况 1. obj.fun() fun中的this->obj,自动指向.前的对 ...
- 使用shiro的密码服务模块
http://jinnianshilongnian.iteye.com/blog/2021439 http://www.cnblogs.com/snidget/p/3817763.html
- background——背景属性
一.背景属性 1.1.背景颜色background-color <style> /*浮动,横向排列*/ div{float: left;} /*background-color属性值支持三 ...
- 跟我一步一步写出MongoDB Web 可视化工具(二)
前言 上篇讲了一些基础,主要注重的是查,包括建立数据库链接.获取数据库.获取表.列出数据库.列出表.列出索引.获取数据等. 本篇依然是基础,注重增改删,废话不多说,咱们开始. 进阶 创建一个数据库和一 ...
- 一个简单的对任意list分页的工具-----PageUtil
一.工具类代码 1 import java.util.List; 2 import java.util.stream.Collectors; 3 4 public class PageUtil< ...
- Robot Framework--Selenium2Library库3
1.介绍 Selenium 自动化测试工具,它主要是用于 Web 应用程序的自动化测试,但并不只局限于此,同时支持所有基于 web 的管理任务自动化.Selenium 的特点: 开源,免费 多浏览 ...
- PHP反射学习总结
反射(Reflection) PHP的反射机制提供了一套反射API,用来访问和使用类.方法.属性.参数和注释等,比如可以通过一个对象知道这个对象所属的类,这个类包含哪些方法,这些方法需要传入什么参数, ...
- os.path官方文档(附翻译)
This module implements some useful functions on pathnames. To read or write files see open(), and fo ...