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将字符串绘制城图 ...
随机推荐
- 016 ppp authentication
Press RETURN to get started! Router>en Router#config t Enter configuration commands, one pe ...
- Tree Operations 打印出有向图中的环
题目: You are given a binary tree with unique integer values on each node. However, the child pointers ...
- iOS 代码安全加固--反编译和代码混淆
一.class-dump反编译 1.将打包的ipa反编译下,.ipa改成.zip,并解压 6.右击—显示包内容,找到如下有个白框黑底的 7.将其复制到桌面xx文件夹中,在终端中输入相关命令 cd 进 ...
- Linux下的文件夹创建命令使用实践
[文章摘要] 本文以实际的C源程序为样例,介绍了Linux下的文件夹创建命令(mkdir)的用法.为相关开发工作的开展提供了故意的參考. [关键词] C语言 Linux 文件夹创建 makefi ...
- 洛谷 P1383 高级打字机==codevs 3333 高级打字机
P1383 高级打字机 18通过 118提交 题目提供者yeszy 标签倍增图论高级数据结构福建省历届夏令营 难度省选/NOI- 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 早苗入手 ...
- 洛谷 P1525 关押罪犯==codevs 1069 关押罪犯[NOIP 2010]
P1525 关押罪犯 513通过 1.4K提交 题目提供者该用户不存在 标签图论并查集NOIp提高组2010 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 咳咳.竟MLE了. 囧.运行时错误 ...
- 获取view宽高
在oncreate()中利用view.getWidth()或是view.getHeiht()来获取view的宽和高,看似没有问题,其实他们去得值是0,并不是你想要的结果? 这是为什么呢? 在调用onc ...
- 如何抓取基于https协议的webservice数据包
方法一:基于Fiddler2等第三方工具(需要在Java端禁用SSL安全检查) 原文拷贝自http://blog.csdn.net/zmxj/article/details/6327775,向原作者表 ...
- Python: PS 滤镜--万花筒效果
本文用 Python 实现 PS 的一种滤镜效果,称为万花筒.也是对图像做各种扭曲变换,最后图像呈现的效果就像从万花筒中看到的一样: 图像的效果可以参考之前的博客: http://blog.csdn. ...
- A bad vacation
My story happened in the winter of 2012, the first year I began to work in Beijing. It was a cold we ...