V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tqknight
V2EX  ›  Python

Python 学习 001——PIL 生成验证码图片

  •  
  •   tqknight · 2017-11-13 11:46:22 +08:00 · 1207 次点击
    这是一个创建于 2411 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #coding:utf-8
    
    import random
    from PIL import Image, ImageDraw, ImageFont, ImageFilter
    
    #Image 负责处理图片
    #ImageDraw 负责处理画笔
    #imageFilter 负责处理滤镜
    
    
    #定义一张图片
    
    
    if __name__ == "__main__":
        print "-------"
        img = Image.new("RGB", (150,50),(255,255,255))
        draw = ImageDraw.Draw(img)
    
    #划线
        for i in range (random.randint(1,10)):
            draw.line(
               [
                   ( random.randint(1,150), random.randint(1,150) ),
                   ( random.randint(1,150), random.randint(1,150) ),],
               fill=(0,0,0)
            )
            
    #画点
        for i in range (random.randint(1,1000)):
            draw.point(
                [ ( random.randint(1,150), random.randint(1,150) )],
                fill=(255,0,0)
            )
    
        #front
        fontList = list("abcdefghijklmnopqrstuvwsyz1234567890")
        c_chars = " ".join(random.sample(fontList,5))
        font = ImageFont.truetype("simsun.ttc",26)
        draw.text((10,10),c_chars, font = font, fill="green")
    
    #扭曲参数
        param = [1-float(random.randint(1,2))/100,
        0,
        0,
        0,
        1-float(random.randint(1,2))/100,
        float(random.randint(1,2))/500,
        0.001,
        float(random.randint(1,2)/100)
        ]
    
    #扭曲
        img = img.transform((150,50), Image.PERSPECTIVE, param)
    #滤镜
        img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
    
        img.show()
    
    
    
    
    
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2960 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:26 · PVG 20:26 · LAX 05:26 · JFK 08:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.