zoukankan      html  css  js  c++  java
  • python PIL 模块

    最近看了下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)    
  • 相关阅读:
    全栈程工程师
    月薪8000的程序员和月薪2万的程序员差别在哪里?
    原型中的访问
    关于 基本类型和复合类型 用== 判断的小问题
    使用原型解决构造函数问题
    前端工程师学习路线 --书籍
    程序员成长之路
    GIT学习(1) Pull和Fetch
    OO面向对象编程:第四单元总结及课程总结
    OO面向对象编程:第三单元总结
  • 原文地址:https://www.cnblogs.com/j657521265/p/10043097.html
Copyright © 2011-2022 走看看