python 中英文验证码实现

本文介绍了如何使用Python生成中英文验证码。通过`RandChar`类生成随机字符,包括数字和字母,以及GB2312编码的汉字。然后利用`VerificationCode`类创建验证码图片,设置字体颜色、大小、背景色,并绘制干扰线。最后,生成指定数量的字符并保存为图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

#coding=utf-8
import Image,ImageDraw,ImageFont


import random

class RandChar(object):
   
    @staticmethod
    def getRandUnicode(supportNum=True,supportLow=True):
        low = 1 if supportNum else 2;
        up = 2 if not supportLow else 3;
        num = random.randint(low,up)
        if num==1:
            return unichr(random.randint(48,57));
        elif num==2:
            return unichr(random.randint(65,90));
        else:
            return unichr(random.randint(97,122));

    @staticmethod
    def getGB2312():
        ''''''
        head = random.randint(0xB0,0xF7);
        body = random.randint(0xA,0xF);
        if body==0xA:
            tail = random.randint(1,0xF);
        elif body==0xF:
            if head ==0xD7:
                tail = random.randint(0,9);
            else:
                tail = random.randint(1,0xe);#这会少几个汉字,不知道gb2312的编码规则,有的从0-e,有的从1-f,这里取得交集
        else:
            tail = random.randint(0,0xF);
        val = "%x"%((head << 8) + (body << 4) + tail);
#        print val
        return val.decode('hex').decode('gb2312')


class VerificationCode(object):
    def __init__(self,fontColor = (0, 0, 0),\
                 size = (100, 40),fontPath = 'SIMSUN.TTC',\
                 bgColor = (255, 255, 255),fontSize = 20):
        '''fontpath:必须是系统所支持的字体,系统支持字体可以在c://windows/fonts下找到'''
        self.fontColor = fontColor;
        self.size = size;
        self.bgColor = bgColor;
        self.fontPath = fontPath;
        self.fontSize = fontSize;
        self.font = ImageFont.truetype(self.fontPath, self.fontSize)
        self.img = Image.new("RGB", size, bgColor);
       
    def drawText(self,pos,txt,fill):
        draw = ImageDraw.Draw(self.img);
        draw.text(pos, txt, fill , self.font)
        del draw
   
    def randRGB(self):
        r = random.randint(0,255);
        g = random.randint(0,255);
        b = random.randint(0,255);
        return (r,g,b);
    def randPoint(self):
        width,higth = self.size;
        return (random.randint(0,width),random.randint(0,higth));
   
    def drawLine(self,num):
        draw = ImageDraw.Draw(self.img);
        for i in range(0,num):
            draw.line((self.randPoint(),self.randPoint()), self.randRGB(), 1);
        del draw;
           
    def getVerificationCode(self,num):
        start = 0;
        gas = 5;
        for i in range(0,num):
            x = start + self.fontSize * i + gas * (i + 1);
            self.drawText((x , random.randint(0,10)), RandChar.getGB2312(), (0,0,0));
        self.drawLine(15);
       
   
    def save(self, path): 
        self.img.save(path)

vc = VerificationCode();
vc.getVerificationCode(4);
#vc.save("%s.jpg"%random.random())
vc.save("1.jpg")
#for i in range(1000):
#    print RandChar.getGB2312()
#val = 'dff7'
#print val.decode('hex').decode('gb2312')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值