1. 程式人生 > >強大的驗證碼生成模塊

強大的驗證碼生成模塊

繁體字 new ase import ini lse 圖片 codes ID

# 首先要安裝PIL庫  pip3 install pillow
from PIL import Image, ImageDraw, ImageFont, ImageFilter

import random
class CheckCode:
    def __init__(self, font_file, font_size=36, width=240, height=60, char_length=4, start_color_num=0,
                 end_color_num=255, is_simple=True):
        self.is_simple 
= is_simple self.font_file = font_file self.font_size = font_size self.width = width self.height = height self.char_length = char_length self.start_num = start_color_num self.end_num = end_color_num # 定義使用Image類實例化一個長為120px,寬為30px,基於RGB的(255,255,255)顏色的圖片
self.image = Image.new(RGB, (self.width, self.height), (255, 255, 255)) # 創建Font對象: self.font = ImageFont.truetype(self.font_file, self.font_size) # 創建Draw對象: self.draw = ImageDraw.Draw(self.image) # 雙下劃綫的變量在類中不能直接訪問起到保護的作用 self.__code_text = []
def get_random_code(self, time=1): """ :param is_simple: 是否包含中文繁體字,默認不包含,True表示不包含 :param time: 返回多少個 :return: 返回一個隨機字符列表 """ is_simple = self.is_simple codes = [] for i in range(time): num = chr(random.randint(0x30, 0x39)) # 隨機生成數字 lowercase = chr(random.randint(0x61, 0x74)) # 隨機生成小寫字母 capcase = chr(random.randint(0x41, 0x5a)) # 隨機生成大寫字母 # Unicode編碼的漢字,帶繁體字 ,共2萬多字 zh = chr(random.randint(0x4e00, 0x9fbf)) # gbk編碼的簡單漢字,無繁體字 head = random.randint(0xb0, 0xf7) body = random.randint(0xa1, 0xf9) # 在head區號為55的那一塊最後5個漢字是亂碼,為了方便縮減下範圍 val = f{head:x}{body:x} ch = bytes.fromhex(val).decode(gb2312) if is_simple: # code = random.choice([ch, num, lowercase, capcase]) code = random.choice([ch, num, lowercase, capcase]) else: code = random.choice([zh, num, lowercase, capcase]) codes.append(code) return codes # 隨機顏色: def rndColor(self, start, end, randomflag=True): """ :param start: :param end: :param randomflag: 是否返回隨機參數, :return: """ return (random.randint(start, end), random.randint(start, end), random.randint(start, end)) def rotate(self): self.image.rotate(random.randint(0, 90), expand=0) # 隨機點 def randPoint(self): return (random.randint(0, self.width), random.randint(0, self.height)) # 隨機線 def randLine(self, num): draw = ImageDraw.Draw(self.image) for i in range(0, num): draw.line([self.randPoint(), self.randPoint()], self.rndColor(0, 255)) del draw # 獲取驗證碼 def get_codes(self): return self.__code_text def draw_pic(self): # 填充每個像素: # 單一背景 color = self.rndColor(0, 255) for x in range(self.width): for y in range(self.height): self.draw.point((x, y), fill=color) # 輸出文字: codes = self.get_random_code(time=self.char_length) for ii in range(self.char_length): code = self.get_random_code()[0] self.__code_text.append(code) self.draw.text([random.randint(int((self.width / 2 - self.font_size / 2) / self.char_length * 2 * ii), int((self.width / 2 - self.font_size / 2) / self.char_length * 2 * ( ii + 1))), random.randint(0, self.height / 4)], code, font=self.font, fill=self.rndColor(0, 255)) # self.rotate() # 畫出隨機線 self.randLine(5) # 模糊: # self.image = self.image.filter(ImageFilter.BLUR) # 保存 self.image.save(code.jpg, jpeg) # 這裏的字體采用能識別中文的字體 font_file = ?C:\Windows\Fonts\simhei.ttf checkcode = CheckCode(font_file=font_file, is_simple=False, char_length=random.randint(3, 7)) checkcode.draw_pic() codes = checkcode.get_codes() print(codes)

強大的驗證碼生成模塊