python PIL 模块

Posted jjcc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)    

 

以上是关于python PIL 模块的主要内容,如果未能解决你的问题,请参考以下文章

python 三种方式实现截屏(详解+完整代码)

Python PIL模块随机生成中文验证码

python中pil如何导入?

Python模块:PIL

python模块之PIL模块(生成随机验证码图片)

python模块之PIL模块(生成随机验证码图片)