用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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM