用python做一个图片验证码

看一下做出来的验证码长啥样

image

验证码分析

1. 有很多点

2. 有很多线条

3. 有字母,有数字

需要用到的模块:

1. random

2. Pillow  (python3中使用pillow)

安装pillow :  pip install pillow

 

pillow的用法:

创建一张图片:

from PIL import Image, ImageDraw, ImageFont, ImageFilter

img = Image.new("RGB", (150,50), (255,255,255))

使用Image.net()方法创建一张图片,“RGB”是指RGB格式的图片, (150,50)指图片的长和高, (255,255,255)是RGB对应的值就是颜色。

在图片上随机画点:

draw.point(
    ( random.randint(0,150), random.randint(0,150)),   #坐标
    fill = (0,0,0)                                     #颜色
)

画点,需要给出点的坐标,就是在什么地方画,坐标怎么算? 图片左上角坐标为(0, 0)

在图片上随机画线条:

draw.line(
        [
            (random.randint(0,150), random.randint(0,150),     #起点
              (random.randint(0,150), random.randint(0,150),     #终点
        ], 
        fill = (0,0,0)    #颜色
)

画线条,需要两个坐标,起点和终点

在图片上写helloworld:

font = ImageFont.truetype("simsun.ttc", 24)   #字体,两个参数,字体,字号
draw.text(5,5), "helloworld", font=font, fill="green")   #文字左上角位置,内容,字体, 颜色

text(5,5)是第一个字左上角的坐标

 

random的用法

这里主要使用randint和sample两个方法

取随机数

import random

random.randint(1, 9)    #从1-9中随机取一个数

从列表中随机取数

>>> li = ['a','b','c',1,3,5,6]
>>> random.sample(li,5)
[6, 3, 'b', 'a', 1]

从列表li中随机取出5个数

 

下面是完整代码

 

from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random
 
class Picture(object):
    def __init__(self, text_str, size, background):
        '''
        text_str: 验证码显示的字符组成的字符串
        size:  图片大小
        background: 背景颜色
        '''
        self.text_list = list(text_str)
        self.size = size
        self.background = background
    
    def create_pic(self):
        '''
        创建一张图片
        '''
        self.width, self.height = self.size
        self.img = Image.new("RGB", self.size, self.background)
        #实例化画笔
        self.draw = ImageDraw.Draw(self.img)
        
    def create_point(self, num, color):
        '''
        num: 画点的数量
        color: 点的颜色
        功能:画点
        '''
        for i in range(num):
            self.draw.point(
                (random.randint(0, self.width), random.randint(0,self.height)),
                fill = color
            )
            
    def create_line(self, num, color):
        '''
        num: 线条的数量
        color: 线条的颜色
        功能:画线条
        '''
        for i in range(num):
            self.draw.line(
                [
                    (random.randint(0, self.width), random.randint(0, self.height)),
                    (random.randint(0, self.width), random.randint(0, self.height))
                ],
                fill = color
            )
            
    def  create_text(self, font_type, font_size, font_color, font_num, start_xy):
        '''
        font_type: 字体
        font_size: 文字大小
        font_color: 文字颜色
        font_num:  文字数量
        start_xy:  第一个字左上角坐标,元组类型,如 (5,5)
        功能: 画文字
        '''
        font = ImageFont.truetype(font_type, font_size)
        self.draw.text(start_xy, " ".join(random.sample(self.text_list, font_num)), font=font, fill=font_color)
        
    def opera(self):
        '''
        功能:给画出来的线条,文字,扭曲一下,缩放一下,位移一下,滤镜一下。
        就是让它看起来有点歪,有点扭。
        '''
        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
        ]
        self.img = self.img.transform(self.size, Image.PERSPECTIVE, params)
        self.img = self.img.filter(ImageFilter.EDGE_ENHANCE_MORE)
        
if __name__ == "__main__":
    strings = "abcdefghjkmnpqrstwxyz23456789ABCDEFGHJKLMNPQRSTWXYZ"
    size = (150,50)
    background = 'white'
    pic = Picture(strings, size, background)
    pic.create_pic()
    pic.create_point(500, (220,220,220))
    pic.create_line(30, (220,220,220))
    pic.create_text("simsun.ttc", 24, (0,0,205), 5, (7,7))
    pic.opera()
    pic.img.show()
posted @ 2016-12-27 18:14  黄小墨  阅读(4460)  评论(0编辑  收藏  举报