码迷,mamicode.com
首页 > 编程语言 > 详细

python PIL 模块

时间:2018-11-30 13:52:49      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:pil   ret   idt   text   htm   div   imp   nbsp   for   

最近看了下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 模块

标签:pil   ret   idt   text   htm   div   imp   nbsp   for   

原文地址:https://www.cnblogs.com/j657521265/p/10043097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!