python生成验证码脚本

最近每天都用python写一个小的脚本,练习使用python语法。

验证码的生成:

这里使用了python的图像处理库PIL,安装PIL的过程中出了一个小麻烦,就使用Pillow-win32的一个文件,具体的我也忘了,可以百度下。

直接看代码:

# -*- coding:utf-8 -*-

from PIL import Image,ImageFont,ImageDraw,ImageFilter
import random

#返回随机字母
def charRandom():
    return chr((random.randint(65,90)))

#返回随机数字
def numRandom():
    return random.randint(0,9)

#随机颜色
def colorRandom1():
    return (random.randint(64,255),random.randint(64,255),random.randint(64,255))

#随机长生颜色2
def colorRandom2():
    return (random.randint(32,127),random.randint(32,127),random.randint(32,127))

width = 60 * 4
height = 60
image = Image.new(‘RGB‘, (width,height), (255,255,255));
#创建font对象
font = ImageFont.truetype(‘Arial.ttf‘,36);

#创建draw对象
draw = ImageDraw.Draw(image)
#填充每一个颜色
for x in range(width):
    for y in range(height):
        draw.point((x,y), fill=colorRandom1())

#输出文字
for t in range(4):
    draw.text((60*t+10,10), charRandom(),font=font, fill=colorRandom2())

#模糊
image = image.filter(ImageFilter.BLUR)
image.save(‘./image/code.jpg‘,‘jpeg‘)

PIL对于图像的处理还是可以的:

这里是官方的文档:http://effbot.org/imagingbook/ 讲的很详细

Image 类是 PIL 库中一个非常重要的类,通过这个类来创建实例可以有直接载入图像文件,读取处理过的图像和通过抓取的方法得到的图像这三种方法。可以对图形进行简单的操作例如旋转、裁剪、缩小放大,滤波等等。

ImageFont模块是字体模块,可以对字体进行设置

上面程序的思想:

1、定义了几个随机获取颜色和数字字母的函数,获得验证码的文字

2、Image.new生成一个黑色的画布,也就是图片,大小60*240

3、ImageFont定义图片上将使用的字体

4、对该图片生成一个draw对象,通过该draw对象,改变图像的像素,使用随见长生的颜色

5、在图片上产生文字,文字也使用随机的颜色

6、对图片进行模糊化处理

7、将图片保存

最后就可以在image文件下得到code.jpg 的图片

扩展:

可以的话,还可以通过draw对象在图片上随机产生几根线条,将验证码变得更难看懂,可以将文字倾斜等等。

image.rotate(random.randint(0,30),expand=0)

def randomLine(self, num):
  draw = ImageDraw.Draw(self.image)
  for i in range(0,num):
  draw.line([self.randomPoint(),self.randomPoint()], self.randomRGB())
  del draw

  

  关于验证码还有很多可以值得看的,之前在用opencv的时候,就想着做一个验证码识别的程序,从而可以实现自动登入系统,不需要每次都手工输入验证码,这好像也是图灵研究的一部分,也是人和机器的区分标志,全自动区分计算机和人类的图灵测试(英语:Completely Automated Public Turing test to tell Computers and Humans Apart,简称CAPTCHA),俗称验证码。具体的可以百度了解。

时间: 2024-08-26 22:56:46

python生成验证码脚本的相关文章

用python生成验证码图片

除了配置好的python环境外,还需要配有python中的PIL库,这是python中专门用来处理图片的库.用传统的pip install 方法或者下载源码 python setup.py install 方法安装该库,很可能会报错(视运行环境不同).可以采用以下方法: 1.下载安装包URL:http://www.pythonware.com/products/pil/index.htm,要下载支持全平台的. 2.解压缩: tar –zxv –f Imaging-1.1.7.tar.gz 3.进

三条代码 搞定 python 生成验证码

C:\Users\DELL>python Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import qrc

python 生成验证码

在工作中经常遇到一些验证码,这些是怎么生成的呢,今天我用Python编写了下 import randomcode = []for i in range(6): if i == random.randint(1,5): code.append(str(random.randint(1,4))) else: tmp = random.randint(65,90) code.append(chr(tmp)) print ''.join(code)

python生成验证码,文字转换为图片

在58或者赶集等一些网站上经常看到手机号是图片格式,或者一些网站的验证码.这些都是动态生成的,今天我们来看一下如何用python把文字生成图片.其实今天主要借助pygame的图像渲染模块,这样比较简单,顺便帮大家复习下pygame这个游戏框架.好啦,直接上代码吧. 环境:python2.7,装有python3的同学也可以测试一下 #coding: UTF-8 #载入必要的模块 import os import pygame from pygame.locals import * #pygame初

Python生成验证码

获取随机字符串 引入PIL包,生成画布. 创建字体,需要使用imagefont.truetype 获取随机背景颜色和字体颜色 将文字写入图像中去 保存图片 代码如下: import random import Image,ImageFilter import ImageFont import ImageDraw #获取随机字符串 def getchar(len=6): #新建元组存储获得的字符串 codelist=[] for i in range(10):#获取数字 codelist.appe

python 生成验证码很简单:random和srting的方法

介绍random和string的有关用法,基础的. sting:------------------获取到0-9十个数字和26个小写大写字母,一共62个. string.printable: >>>string.printable >>>'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./: ;<=>[email protected][

使用 Python 生成验证码(CAPTCHA)

CAPTCHA 图像 Python 数据可视化编程实战代码 点击查看详细内容 from PIL import Image, ImageDraw, ImageFont import random import string class SimpleCaptchaException(Exception): pass class SimpleCaptcha(object): def __init__(self, length=5, size=(200, 100), fontsize=36, rando

Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed

最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows. 所以在安装好pil之后就开始写,就按照题目所说出现了The _imagingft C module is not installed 错误,找了很多建议,最后确定在windows下应该用pillpw.下载地址 点击打开链接 找到 Pillow?2.5.2.win32?py2.7.exe因为我用的是python2.7和win32系统,所以就应该下载这个,大

Python Show-Me-the-Code 第 0010 题 生成验证码图片

第 0010 题:使用 Python 生成类似于下图中的字母验证码图片 阅读资料 思路:先随机生成验证码,然后用Python的PIL库画出这个激活码的图片,具体点就是创建画布,加验证码的字上去,增加噪点进行干扰,再进行模糊处理,接着保存到名字为验证码的图片中. 0010.生成验证码图片.py #!/usr/bin/env python #coding: utf-8 import Image, ImageDraw, ImageFont, ImageFilter import string, ran