1. 程式人生 > >python PIL 模塊

python PIL 模塊

col [] edraw () imp pan pytho ext char

最近看了下PIL模塊,看了下別人是如何生成驗證碼數據集

參考 https://www.cnblogs.com/tsboy/p/8862707.html

import random
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

def RandomColor():
    c1 = random.randint(0,255)
    c2 = random.randint(0,255)
    c3 = random.randint(0,255)
    return (c1,c2,c3)
    
def
RandomStr(): random_number = str(random.randint(0,9)) random_low = chr(random.randint(97,122)) random_upper = chr(random.randint(65,90)) random_str = random.choice([random_number,random_low,random_upper]) return random_str #sum_number = 驗證碼總個數 draw = ImageDraw.Draw(im01) draw.line((0,0) +im01.size, fill=128)
def Drawsomethings(k): width = 150 height = 30 image = Image.new(RGB,(150,30),RandomColor()) draw = ImageDraw.Draw(image) #font = ImageFont.truetype("simsun.ttc",size=random.randint(20,30)) #寫驗證碼 for i in range(5): temp = [] random_char = RandomStr()
#draw.text((10+i*30, 0),random_char , RandomColor(), font=font) draw.text((0+i*30, 0),random_char , RandomColor(), font=font) temp.append(random_char) #畫線 for i in range(5): x1=random.randint(0,width) x2=random.randint(0,width) y1=random.randint(0,height) y2=random.randint(0,height) draw.line((x1,y1,x2,y2),fill=RandomColor()) #畫點 for i in range(30): draw.point([random.randint(0, width), random.randint(0, height)], fill=RandomColor()) x = random.randint(0, width) y = random.randint(0, height) draw.arc((x, y, x + 4, y + 4), 0, 90, fill=RandomColor()) name = str(k) + .png image.save(name, png) if __name__ == __main__: for i in range(100): Drawsomethings(k = i)

python PIL 模塊