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
RadAsm
V2EX  ›  Python

生成倾斜验证码问题

  •  
  •   RadAsm · 2015-10-28 16:45:03 +08:00 · 4306 次点击
    这是一个创建于 3074 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想生成字母是倾斜的验证码,代码如下:

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    
    #生成验证码图片
    
    from PIL import Image,ImageDraw,ImageFont,ImageFilter
    
    import random
    
    #chr 转成相应的 ascll 值
    def rndChar():
        return chr(random.randint(65,90))
    
    def rndColor():
        return (random.randint(64,255),random.randint(64,255),random.randint(64,255))
    
    def rndColor2():
        return (random.randint(32,127),random.randint(32,127),random.randint(32,127))
    
    width=60*4
    height=60
    image=Image.new('RGB',(width,height),(255,255,255))
    
    
    font = ImageFont.truetype('Arial.ttf',36)
    
    draw = ImageDraw.Draw(image)
    for x in range(width):
        for y in range(height):
            draw.point((x,y),fill=rndColor())
    
    word=''
    for t in range(4):
        imaget = Image.new('RGB',(60,60),(255,255,255))
        drawt = ImageDraw.Draw(imaget)
        for x in range(60):
            for y in range(60):
                drawt.point((x,y),fill=rndColor())
        c = rndChar()
        word=word+c
        drawt.text((12,12),c,font=font,fill=rndColor2())
        imaget = imaget.rotate(random.randint(-30,30))
        image.paste(imaget,(60*t,0))
    
    # for t in range(4):
    #   draw.text((60*t+10,10),rndChar(),font=font,fill=rndColor2())
    
    image = image.filter(ImageFilter.BLUR)
    
    image.save('hehe.jpg','jpeg')       
    
    print('word is %s ' % (word))
    

    主要是想其中的文字能够倾斜。。。

    结果生成这样了:
    生成的图片

    怎么才能去除那些嘿嘿的东西呢。。。

    第 1 条附言  ·  2015-10-28 17:23:06 +08:00

    按照 @gowithwind 同学,扫描后可以了

    for x in range(width):
        for y in range(height):
            c=image.getpixel((x,y))
            if c==(0,0,0):
                draw.point((x,y),fill=rndColor())
    

    :-D

    6 条回复    2015-10-29 16:23:01 +08:00
    gowithwind
        1
    gowithwind  
       2015-10-28 16:50:13 +08:00
    先用随机颜色填充背景.
    RadAsm
        2
    RadAsm  
    OP
       2015-10-28 16:53:20 +08:00
    @gowithwind
    已经先填充好了,但是因为之后我的 60*60 的图片是单独生成,并粘贴到原来的图片上的,所以之前的随机填充色没了。。。
    gowithwind
        3
    gowithwind  
       2015-10-28 16:55:33 +08:00   ❤️ 1
    @RadAsm
    image=Image.new('RGB',(width,height),(255,255,255))
    这里随机填充
    gowithwind
        4
    gowithwind  
       2015-10-28 17:00:22 +08:00
    @RadAsm
    搞错了.不好意思.
    一个方案是可以再扫描下图像,把黑色换成随机色.(出现黑色应该是旋转图像用黑色来填补了.)
    RadAsm
        5
    RadAsm  
    OP
       2015-10-28 17:20:11 +08:00
    @gowithwind 嗯,可以了,谢谢你!😊
    fy
        6
    fy  
       2015-10-29 16:23:01 +08:00
    不错,我最近在想如果直接生成 svg ,是不是比生成图片更加轻量呢?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5329 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 08:17 · PVG 16:17 · LAX 01:17 · JFK 04:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.