国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python 隨機(jī)生成中文驗(yàn)證碼的實(shí)例代碼

2020-02-23 04:51:54
字體:
供稿:網(wǎng)友

python代碼
代碼如下:
 # -*- coding: utf-8 -*-

 import Image,ImageDraw,ImageFont

 import random

 import math, string  

 class RandomChar():

   """用于隨機(jī)生成漢字"""

   @staticmethod

   def Unicode():

     val = random.randint(0x4E00, 0x9FBF)

     return unichr(val)  

   @staticmethod

   def GB2312():

     head = random.randint(0xB0, 0xCF)

     body = random.randint(0xA, 0xF)

     tail = random.randint(0, 0xF)

     val = ( head << 8 ) | (body << 4) | tail

     str = "%x" % val

     return str.decode('hex').decode('gb2312')  

  

 class ImageChar():

   def __init__(self, fontColor = (0, 0, 0),

                      size = (100, 40),

                      fontPath = 'wqy.ttc',

                      bgColor = (255, 255, 255),

                      fontSize = 20):

     self.size = size

     self.fontPath = fontPath

     self.bgColor = bgColor

     self.fontSize = fontSize

     self.fontColor = fontColor

     self.font = ImageFont.truetype(self.fontPath, self.fontSize)

     self.image = Image.new('RGB', size, bgColor)  

   def rotate(self):

     self.image.rotate(random.randint(0, 30), expand=0)  

   def drawText(self, pos, txt, fill):

     draw = ImageDraw.Draw(self.image)

     draw.text(pos, txt, font=self.font, fill=fill)

     del draw  

   def randRGB(self):

     return (random.randint(0, 255),

            random.randint(0, 255),

            random.randint(0, 255))  

   def randPoint(self):

     (width, height) = self.size

     return (random.randint(0, width), random.randint(0, height))  

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 上饶县| 哈巴河县| 高安市| 商水县| 临安市| 高雄市| 弋阳县| 惠安县| 兰州市| 同江市| 宜宾市| 龙胜| 徐州市| 禄劝| 和顺县| 黑河市| 施甸县| 凉城县| 丰原市| 彰化市| 扬州市| 秭归县| 乳源| 乌兰浩特市| 怀仁县| 巴中市| 北川| 淮北市| 蓬安县| 华池县| 且末县| 尚志市| 新乡县| 福泉市| 平原县| 镇赉县| 崇义县| 淅川县| 宜宾县| 宣威市| 公安县|