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

首頁 > 編程 > Python > 正文

python生成隨機驗證碼(中文驗證碼)示例

2020-02-23 05:16:54
字體:
來源:轉載
供稿:網友

代碼如下:
# -*- coding: utf-8 -*-
import Image,ImageDraw,ImageFont
import random
import math, string 

class RandomChar():
  """用于隨機生成漢字"""
  @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)) 

  def randLine(self, num):
    draw = ImageDraw.Draw(self.image)
    for i in range(0, num):
      draw.line([self.randPoint(), self.randPoint()], self.randRGB())
    del draw 

  def randChinese(self, num):
    gap = 5
    start = 0
    for i in range(0, num):
      char = RandomChar().GB2312()
      x = start + self.fontSize * i + random.randint(0, gap) + gap * i

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石楼县| 门头沟区| 苗栗市| 开平市| 兴山县| 中江县| 桑日县| 固原市| 乌鲁木齐市| 丰宁| 秭归县| 鄢陵县| 赫章县| 高邑县| 抚顺县| 昌图县| 南皮县| 竹北市| 弋阳县| 河南省| 湘潭县| 墨竹工卡县| 文昌市| 栾川县| 杭锦后旗| 保靖县| 米泉市| 华亭县| 祁门县| 无棣县| 兴宁市| 定日县| 墨竹工卡县| 铜鼓县| 新昌县| 湟源县| 婺源县| 余姚市| 鄯善县| 玉田县| 潞城市|