Python3.4的Pillow库实现验证码图片
转自 http://blog.csdn.net/bin381/article/details/41969493
from PIL import Image,ImageDraw, ImageFont, ImageFilter
import random
_letter_cases = 'abcdefghjkmnpqrstuvwxy'
_upper_cases = _letter_cases.upper()
_numbers = ''.join(map(str, range(3, 10)))
init_chars = ''.join((_letter_cases, _upper_cases, _numbers)) def creat_validata_code(size=(120,30),chars=init_chars,img_type='jpg',
mode='RGB',bg_color=(255,255,255),fg_color=(0,0,255),
font_size=18,font_type='arial.ttf',
length=4,draw_lines=True,n_line=(1,2),
draw_points=True,point_chance=2):
width,height = size;
img = Image.new(mode, size, bg_color);
draw = ImageDraw.Draw(img) def get_chars():
return random.sample(chars,length) def creat_line():
line_num = random.randint(*n_line)#sign that the param is a list for i in range(line_num):
begin = (random.randint(0, size[0]), random.randint(0, size[1]))
end = (random.randint(0, size[0]), random.randint(0, size[1]))
draw.line([begin, end], fill=(0, 0, 0)) def create_points():
chance = min(100, max(0, int(point_chance)))
for w in range(width):
for h in range(height):
tmp = random.randint(0, 100)
if tmp > 100 - chance:
draw.point((w, h), fill=(0, 0, 0)) def create_strs():
c_chars = get_chars()
strs = ' %s ' % ' '.join(c_chars)
font = ImageFont.truetype(font_type, font_size)
font_width, font_height = font.getsize(strs)
draw.text(((width - font_width) / 3, (height - font_height) / 3),
strs, font=font, fill=fg_color)
return ''.join(c_chars) if draw_lines:
creat_line()
if draw_points:
create_points()
strs = create_strs() params = [1 - float(random.randint(1, 2)) / 100,
0,
0,
0,
1 - float(random.randint(1, 10)) / 100,
float(random.randint(1, 2)) / 500,
0.001,
float(random.randint(1, 2)) / 500
]
img = img.transform(size, Image.PERSPECTIVE, params)
img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
return img, strs if __name__ == '__main__':
code_img,capacha_code= creat_validata_code()
code_img.save('xx.jpg','JPEG')
参考链接:
Python3.4的Pillow库实现验证码图片的更多相关文章
- PHP:GD库 生成验证码图片
文章来源:http://www.cnblogs.com/hello-tl/p/7592998.html <?php /** * __construct($new):构造函数创建一张图片$new- ...
- 纯代码系列:Python实现验证码图片(PIL库经典用法用法,爬虫12306思路)
现在的网页中,为了防止机器人提交表单,图片验证码是很常见的应对手段之一.这里就不详细介绍了,相信大家都遇到过. 现在就给出用Python的PIL库实现验证码图片的代码.代码中有详细注释. #!/usr ...
- python中用Pillow库进行图片处理
一.Python中 PIL 图像处理库简介 PIL可以做很多和图像处理相关的事情: 图像归档(Image Archives).PIL非常适合于图像归档以及图像的批处理任务.你可以使用PIL创建缩略图, ...
- 尝试用python开发一款图片压缩工具1:尝试 pillow库
开发目的 我经常使用图片.公众号文章发文也好,还是生活中要使用素材.图片是一种比文字更加直观的载体.但是图片更加占用带宽,很多软件都对图片有大小限制.图片太大也会影响加载速度.我试过几款图片压缩工具, ...
- Python3.x:第三方库简介
Python3.x:第三方库简介 环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex ...
- Python 使用Pillow模块生成验证码
1.安装 pip3 install pillow 2.使用步骤 生成验证码和验证字符串 绘制图片,将验证码放入session中 将图片返回给页面 3.代码demo #!/usr/bin/env pyt ...
- python2 生成验证码图片
使用pillow或者pil库编写 #coding:utf-8 #use pillow or pil try: from PIL import Image, ImageDraw, ImageFont, ...
- phpcms V9 后台验证码图片不显示
某个网站在本地运行成功,上传到服务器上后,发现后台登陆的验证码图片不显示 根据网上提供的解决方案, 网站路径变量web_path没问题 database.system的配置路径没问题 apache的G ...
- Python利用PIL生成随机验证码图片
安装pillow: pip install pillow PIL中的Image等模块提供了创建图片,制作图片的功能,大致的步骤就是我们利用random生成6个随机字符串,然后利用PIL将字符串绘制城图 ...
随机推荐
- laravel 实时facade
实时facade 创建一个目录叫services 创建一个weibo类 <?php namespace App\Services; class weibo { protected $http; ...
- {head first} --- networking 1
Head first系列的书确实非常好,深入浅出解说网络的组成.让曾经那些生涩的概念生动起来. Chapter 1 维修物理网络 CAT5电缆: 两端为RJ-45接头(水晶头).内部为UTP(非屏蔽双 ...
- C#高级编程五十二天----有序列表
有序列表 假设须要基于对全部集合排序,就能够使用SortedList<TKey,TValue>类.这个类依照键给元素排序.这个集合中的值和键都能够使用随意类型. 以下的样例创建了一个有序列 ...
- php把时间计算成几分钟前,几小时前,几天前的函数
function time_tran($the_time){ $now_time = date("Y-m-d H:i:s",time()+8*60*60); $now_time = ...
- iOS平台加入Google Admob -1/2(Unity3D开发之七)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com.谢谢! 原文地址: http://www.cocos2dev.com/?p=567 Unity调用iOS还是非 ...
- 【bzoj2743】[HEOI2012]采花
树状数组 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring& ...
- python3 使用http.server模块 搭建一个简易的http服务器
from http.server import HTTPServer, BaseHTTPRequestHandler import json data = {'result': 'this is a ...
- struts2 下载
struts 官网 : https://struts.apache.org/
- Robot Framework 初学者上手资料
首先要声明一下这是从http://www.cnblogs.com/yufeihlf/p/5949984.html拷贝的. 在这里只是自己的一个笔记,方便日后添加.修改内容. 总结下Robot Fram ...
- YTU 2626: B 统计程序设计基础课程学生的平均成绩
2626: B 统计程序设计基础课程学生的平均成绩 时间限制: 1 Sec 内存限制: 128 MB 提交: 427 解决: 143 题目描述 程序设计基础课程的学生成绩出来了,老师需要统计出学生 ...