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")
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
对python读取zip压缩文件里面的csv数据实例详解
![](https://img.aspxhome.com/file/2023/7/103617_0s.jpg)
详解Python 数据库 (sqlite3)应用
![](https://img.aspxhome.com/file/2023/5/105275_0s.png)
Python 串口读写的实现方法
![](https://img.aspxhome.com/file/2023/4/71994_0s.jpg)
C#使用SqlServer作为日志数据库的设计与实现
![](https://img.aspxhome.com/file/2023/3/108673_0s.png)
Python爬虫练习汇总
![](https://img.aspxhome.com/file/2023/9/134909_0s.png)
Oracle中PL/SQL复合数据类型
css+js实现部分区域高亮可编辑遮罩层
![](https://img.aspxhome.com/file/2023/4/56474_0s.gif)
python下MySQLdb用法实例分析
用1行Python代码识别身份证信息实例
![](https://img.aspxhome.com/file/2023/8/87898_0s.jpg)
基于Python3编写一个GUI翻译器
![](https://img.aspxhome.com/file/2023/7/80637_0s.gif)
MySQL中CURRENT_TIMESTAMP的使用方式
![](https://img.aspxhome.com/file/2023/6/78206_0s.png)
python如何判断文件存在方式
教你用eclipse连接mysql数据库
![](https://img.aspxhome.com/file/2023/4/103564_0s.png)
python分布式环境下的限流器的示例
超全MySQL学习笔记
![](https://img.aspxhome.com/file/2023/1/112351_0s.jpg)
Python文件操作之合并文本文件内容示例代码
![](https://img.aspxhome.com/file/2023/5/92315_0s.png)
Go语言中defer语句的用法
![](https://img.aspxhome.com/file/2023/3/92023_0s.png)
PHP实现向关联数组指定的Key之前插入元素的方法
python开发实时可视化仪表盘的示例
![](https://img.aspxhome.com/file/2023/0/112370_0s.gif)