国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 石柱| 高唐县| 青岛市| 淄博市| 铁岭县| 龙州县| 南阳市| 新营市| 翁牛特旗| 莎车县| 通榆县| 临泽县| 嘉峪关市| 桐乡市| 乃东县| 竹溪县| 永吉县| 梓潼县| 靖宇县| 中山市| 阿合奇县| 建瓯市| 中超| 正定县| 石城县| 区。| 拜泉县| 徐闻县| 自贡市| 商都县| 汾阳市| 穆棱市| 宁远县| 英德市| 故城县| 黄石市| 威信县| 喜德县| 通江县| 江永县| 鄢陵县|