pliiow模塊生成驗證碼

使用pillow生成驗證碼

1.生成驗證碼

封裝成類:

class Verification(object):
    def __init__(self):
        '''
        width:驗證碼的長度
        height:驗證碼的寬度
        char_length:驗證碼的長度
        font_file:字體格式
        font_size:字體大小
        '''
        self.width = 200
        self.height = 30
        self.char_length = 5
        self.font_file = "KumoFont.ttf"
        self.font_size = 30

    def rndChar(self):
        '''隨機生成字母'''
        if random.randint(0, 9) % 2 == 0:
            return chr(random.randint(65, 90))  # ASII碼65-90是大寫字母
        return chr(random.randint(97, 122))  # ASCII碼97-122是小寫字母

    def rndColor(self):
        '''隨機生成顏色'''
        return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    def rndInt(self):
        '''隨機生成數字'''
        return chr(random.randint(48, 57))

    def draw_txt(self, code, draw):
        '''畫字符'''
        font = ImageFont.truetype(self.font_file, self.font_size)
        for i in range(self.char_length):
            if random.randint(0, 9) % 2 == 0:
                # if i % 2 == 0:
                char = self.rndChar()
                code.append(char)
            else:
                char = self.rndInt()
                code.append(char)
            h = random.randint(0, 4)
            draw.text([i * self.width / self.char_length, h], char, font=font, fill=self.rndColor())

    def draw_point(self, draw):
        '''畫干擾點'''
        for i in range(30):
            draw.point([random.randint(0, self.width), random.randint(0, self.height)], fill=self.rndColor())

    def draw_cycle(self, draw):
        '''畫干擾圓'''
        for i in range(20):
            draw.point([random.randint(0, self.width), random.randint(0, self.height)], fill=self.rndColor())
            x = random.randint(0, self.width)
            y = random.randint(0, self.height)
            draw.arc((x, y, x + 4, y + 4), 0, 90, fill=self.rndColor())

    def draw_line(self, draw):
        '''畫干擾線'''
        for i in range(5):
            x1 = random.randint(0, self.width)
            y1 = random.randint(0, self.height)
            x2 = random.randint(0, self.width)
            y2 = random.randint(0, self.height)

            draw.line((x1, y1, x2, y2), fill=self.rndColor())

    def produce(self):
        '''生成驗證碼圖片和驗證碼'''
        img = Image.new(mode='RGB', size=(self.width, self.height), color=(255, 255, 255))
        draw = ImageDraw.Draw(img, mode='RGB')
        code = []
        self.draw_txt(code, draw)
        self.draw_point(draw)
        self.draw_cycle(draw)
        self.draw_line(draw)
        img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
        return img, ''.join(code)

# 將生成驗證碼封裝爲函數
def check_code(width=120, height=30, char_length=5, font_file='KumoFont.ttf', font_size=28):
    code = []
    img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img, mode='RGB')

    def rndChar():
        """
        生成隨機字母
        :return:
        """
        if random.randint(0, 9) % 2 == 0:
            return chr(random.randint(65, 90))  # ASII碼65-90是大寫字母
        return chr(random.randint(97, 122))  # ASCII碼97-122是小寫字母

    def rndColor():
        """
        生成隨機顏色
        :return:
        """
        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))

    def rndInt():
        '''隨機生成數字'''
        return chr(random.randint(48, 57))

    # 寫文字
    font = ImageFont.truetype(font_file, font_size)
    for i in range(char_length):
        if random.randint(0, 9) % 2 == 0:
            # if i % 2 == 0:
            char = rndChar()
            code.append(char)
        else:
            char = rndInt()
            code.append(char)
        h = random.randint(0, 4)
        draw.text([i * width / char_length, h], char, font=font, fill=rndColor())

    # 寫干擾點
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

    # 寫干擾圓圈
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())

    # 畫干擾線
    for i in range(5):
        x1 = random.randint(0, width)
        y1 = random.randint(0, height)
        x2 = random.randint(0, width)
        y2 = random.randint(0, height)
c
        draw.line((x1, y1, x2, y2), fill=rndColor())

    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
    return img, ''.join(code)

封裝成函數:

def check_code(width=200, height=30, char_length=5, font_file='KumoFont.ttf', font_size=30):
    code = []
    img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img, mode='RGB')

    def rndChar():
        """
        生成隨機字母
        :return:
        """
        if random.randint(0, 9) % 2 == 0:
            return chr(random.randint(65, 90))  # ASII碼65-90是大寫字母
        return chr(random.randint(97, 122))  # ASCII碼97-122是小寫字母

    def rndColor():
        """
        生成隨機顏色
        :return:
        """
        return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    def rndInt():
        '''隨機生成數字'''
        return chr(random.randint(48, 57))

    # 寫文字
    font = ImageFont.truetype(font_file, font_size)
    for i in range(char_length):
        if random.randint(0, 9) % 2 == 0:
            # if i % 2 == 0:
            char = rndChar()
            code.append(char)
        else:
            char = rndInt()
            code.append(char)
        h = random.randint(0, 4)
        draw.text([i * width / char_length, h], char, font=font, fill=rndColor())

    # 寫干擾點
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

    # 寫干擾圓圈
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())

    # 畫干擾線
    for i in range(5):
        x1 = random.randint(0, width)
        y1 = random.randint(0, height)
        x2 = random.randint(0, width)
        y2 = random.randint(0, height)

        draw.line((x1, y1, x2, y2), fill=rndColor())

    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
    return img, ''.join(code)

2.查看圖片

1.直接在本地圖片查看器打開

img,code = Verification().produce()
print(code)
img.show()

2.保存到內存中再讀取

img,code = Verification().produce()
from io import BytesIO
stream = BytesIO()    #將圖片存到內存中
img.save(stream,'png')
stream.getvalue()

3.保存到文件中

img,code = Verification().produce()
with open("驗證碼.png","wb") as f:
	img.save(f,format='png')

測試如下:

img,code = Verification().produce()
print(code)
img.show()

運行結果:
在這裏插入圖片描述

ps:字體下載地址:http://font.chinaz.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章