PIL(Python Image Library)生成验证码
# -*- coding: utf-8 -*-
#导入三个模块
import Image,ImageDraw,ImageFont
import random
import math
'''基本功能'''
#图片宽度
width = 100
#图片高度
height = 40
#背景颜色
bgcolor = (255,255,255)
#生成背景图片
image = Image.new('RGB',(width,height),bgcolor)
#加载字体
font = ImageFont.truetype('FreeSans.ttf',30)
#字体颜色
fontcolor = (0,0,0)
#产生draw对象,draw是一些算法的集合
draw = ImageDraw.Draw(image)
#画字体,(0,0)是起始位置
draw.text((0,0),'1234',font=font,fill=fontcolor)
#释放draw
del draw
#保存原始版本
image.save('1234_1.jpeg')
'''演示扭曲,需要新建一个图片对象'''
#新图片
newImage = Image.new('RGB',(width,height),bgcolor)
#load像素
newPix = newImage.load()
pix = image.load()
offset = 0
for y in range(0,height):
offset += 1
for x in range(0,width):
#新的x坐标点
newx = x + offset
#你可以试试如下的效果
#newx = x + math.sin(float(y)/10)*10
if newx < width:
#把源像素通过偏移到新的像素点
newPix[newx,y] = pix[x,y]
#保存扭曲后的版本
newImage.save('1234_2.jpeg')
'''形变一下'''
#x1 = ax+by+c
#y1 = dx+ey+f
newImage = image.transform((width+30,height+10), Image.AFFINE, (1,-0.3,0,-0.1,1,0))
newImage.save('1234_3.jpeg')
'''画干扰线,别画太多,免得用户都看不清楚'''
#创建draw,画线用
draw = ImageDraw.Draw(newImage)
#线的颜色
linecolor= (0,0,0)
for i in range(0,15):
#都是随机的
x1 = random.randint(0,width)
x2 = random.randint(0,width)
y1 = random.randint(0,height)
y2 = random.randint(0,height)
draw.line([(x1, y1), (x2, y2)], linecolor)
#保存到本地
newImage.save('1234_4.jpeg')
PIL(Python Image Library)生成验证码的更多相关文章
- python爬虫之浅析验证码
一.什么是验证码? 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”( ...
- Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed
最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows. 所以在安装好pil之后就开始写,就按照题目所说出现了Th ...
- PIL:Python Imaging Library(图像处理标准库)和Qrcode:二维码生成
安装PIL Mac或Linux安装命令:sudo easy_install PIL 如果报错:fatal error: 'freetype/fterrors.h' file not found Mac ...
- Python随机生成验证码的两种方法
Python随机生成验证码的方法有很多,今天给大家列举两种,大家也可以在这个基础上进行改造,设计出适合自己的验证码方法方法一:利用range Python随机生成验证码的方法有很多,今天给大家列举两种 ...
- Python实现网站注册验证码生成类
# -*- coding:utf-8 -*- ''' Created on 2017年4月7日 @author: Water ''' import os import random import st ...
- Python 使用Pillow模块生成验证码
1.安装 pip3 install pillow 2.使用步骤 生成验证码和验证字符串 绘制图片,将验证码放入session中 将图片返回给页面 3.代码demo #!/usr/bin/env pyt ...
- python的random模块(生成验证码)
python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) # ...
- Windows环境下安装PIL(Python Imaging Library)库
微信小程序--跳一跳最近火了一把,于是整了个辅助进行试玩,不过在运行程序过程中出现了个报错如图所示: 显然是缺少PIL(Python Imaging Library)库文件,于是通过pip命令行进行安 ...
- python快速生成验证码
利用python库random,string生成大小写字母和数字的随机验证码 import random import string def generate_code(bit_num): ''' : ...
随机推荐
- [记录]ns_error_unexpected firefox tinymce
参考: Form not saving, NS_ERROR_UNEXPECTED Error: NS_ERROR_UNEXPECTED #794 NS_ERROR_UNEXPECTED on Fir ...
- scala学习笔记:理解函数
定义一个函数: scala> def foo(x:Int)=x*2 foo: (x: Int)Int 可以采用匿名参数: scala> def foo:((Int)=>Int) = ...
- Ubuntu Update-rc.d命令详细介绍
http://www.jb51.net/os/Ubuntu/182768.html Ubuntu或者Debian系统中update-rc.d命令,是用来更新系统启动项的脚本.这些脚本的链接位于/etc ...
- ### About Multi-Object Tracking
点击查看Evernote原文. #@author: gr #@date: 2014-10-17 #@email: forgerui@gmail.com Multi_Object Tracking Fr ...
- iBeacon 开发笔记
iBeacon开发笔记 2015.10.19 airlocate ========= airlocate显示如何使用这个监控范围clbeaconregions. 代码还提供了一个例子,你如何能校准和配 ...
- Headfirst设计模式的C++实现——组合模式(Composite)
menu_component.h #ifndef _MENU_COMPONENT_H_ #define _MENU_COMPONENT_H_ #include <string> class ...
- WIN7中oracle10g的安装注意事项
1.本次安装数据库版本为10.2.0.1,操作系统版本为windows7 32位 2.注意在"setup.exe"中以右键属性后,设置以兼容模式及以管理员身份运行该程序:在%安装文 ...
- js清空web用户控件的值
假设你的用户控件里面有: <asp:DropDownList ID="DropDownList1" runat="server"> <asp: ...
- yii2 model常用验证规则
//字段必填[['username'],'required','message'=>'{attribute}不能为空!'][['username','password'], 'required' ...
- 解决php json_encode 出现的中文转码、乱码问题
// 防止json中文转码 function jsonEncodeWithCN($data) { return preg_replace("/\\\u([0-9a-f]{4})/ie&quo ...