Python 随机生成中文验证码的实例代码

时间:2022-12-15 23:17:34 

python代码


 # -*- 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

       self.drawText((x, random.randint(-5, 5)), RandomChar().GB2312(), self.randRGB())

       self.rotate()

     self.randLine(18)  

   def save(self, path):

     self.image.save(path)


调用方法


 ic = ImageChar(fontColor=(100,211, 90))

 ic.randChinese(4)

 ic.save("1.jpeg")


标签:Python,随机,中文,验证码
0
投稿

猜你喜欢

  • 对python读取zip压缩文件里面的csv数据实例详解

    2022-04-14 10:48:57
  • 详解Python 数据库 (sqlite3)应用

    2024-01-21 06:14:46
  • Python 串口读写的实现方法

    2021-04-24 13:25:12
  • C#使用SqlServer作为日志数据库的设计与实现

    2024-01-17 05:10:12
  • Python爬虫练习汇总

    2023-04-27 03:16:37
  • Oracle中PL/SQL复合数据类型

    2024-01-22 07:17:47
  • css+js实现部分区域高亮可编辑遮罩层

    2024-02-25 08:49:23
  • python下MySQLdb用法实例分析

    2024-01-18 11:50:27
  • 用1行Python代码识别身份证信息实例

    2022-04-28 12:57:27
  • 基于Python3编写一个GUI翻译器

    2022-07-07 07:57:54
  • MySQL中CURRENT_TIMESTAMP的使用方式

    2024-01-12 20:10:29
  • python如何判断文件存在方式

    2023-01-23 12:58:37
  • 教你用eclipse连接mysql数据库

    2024-01-19 23:30:41
  • python分布式环境下的限流器的示例

    2023-07-11 19:25:38
  • 超全MySQL学习笔记

    2024-01-25 20:21:45
  • Python文件操作之合并文本文件内容示例代码

    2021-06-24 13:58:05
  • Go语言中defer语句的用法

    2023-07-07 11:04:17
  • PHP实现向关联数组指定的Key之前插入元素的方法

    2023-07-14 08:41:33
  • python开发实时可视化仪表盘的示例

    2022-04-23 15:33:44
  • Go mod包管理工具详解

    2024-04-30 10:08:11
  • asp之家 网络编程 m.aspxhome.com