在Python项目中怎样生成中文的验证码
Admin 2022-07-14 群英技术资讯 790 次浏览
关于“在Python项目中怎样生成中文的验证码”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。PIL是Python Imaging Library的简称,PIL是一个Python处理图片的库,提供了一系列模块和方法,比如:裁切,平移,旋转,改变尺寸等等。已经是Python平台事实上的图像处理标准库了。PIL功能非常强大,但API却非常简单易用。
PIL有如下几个模块:Image模块、ImageChops模块、ImageCrackCode模块、ImageDraw模块、ImageEnhance模块、ImageFile模块、ImageFileIO模块、ImageFilter模块、ImageFont模块、ImageGrab模块、ImageOps模块、ImagePath模块、ImageSequence模块、ImageStat模块、ImageTk模块、ImageWin模块、PSDraw模块。
在PIL库中,任何一个图像都是用Image对象来表示的,所以要加载一张图片,最简单的形式如下:
from PIL import Image
image = Image.open("1.jpeg")
在PIL库中,最常用的模块有Image,ImageDraw,ImageEnhance,ImageFile等。
PIL的下载地址为:http://www.pythonware.com/products/pil/
接下来学习PIL中一些常见的操作:
使用resize方法可以方便的对图片进行缩放
from PIL import Image
image = Image.open("1.jpeg")
new_img = image.resize((256,256),Image.BILINEAR)
new_img.save("2.jpeg")
rotate方法可以进行旋转操作
from PIL import Image
image = Image.open("1.jpeg")
new_img = image.rotate(45)
new_img.save("2.jpeg")
PIL库中的ImageDraw模块提供了图形绘制的基本功能,可以绘制直线,弧线,椭圆,矩形等等。
from PIL import Image,ImageDraw
image = Image.open("2.jpeg")
draw = ImageDraw.Draw(image)
width,height = image.size
draw.line(((0,0),(width-1,height-1)),fill=255)
draw.line(((0,height-1),(width-1,0)),fill=255)
image.save("2.jpeg")
from PIL import Image,ImageDraw
image = Image.open("2.jpeg")
draw = ImageDraw.Draw(image)
width,height = image.size
draw.arc((0,0,width-1,height-1),0,360,fill=255)
image.save("2.jpeg")
更多方法可以参见帮助文档。
现在我们来研究利用Python如何生成中文验证码。其实这个问题简单,我们都知道验证码一般是用来防止网络机器
人采用无限次数的登录尝试破解密码,那么我们需要对这张图片随机生成中文字符来验证,一般来说会把图片的字表
现得很模糊,这样有效地防止机器人识别。所以我们除了要随机生成汉字,还要加入一些干扰线条等等。
# -*- coding: utf-8 -*-
from PIL 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 = 'SIMSUN.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)
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())
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")
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
pygame是为开发2D游戏而设计的Python跨平台模块,开发人员利用pygame模块中定义的接口,可以方便快捷地实现诸如图形用户界面创建、图形和图像的绘制、用户键盘和鼠标操作的监听以及播放音频等游戏中常用的功能。
这篇文章主要介绍了聊聊机器学习的标准化、归一化、正则化、离散化和白化,帮助大家更好的理解和学习使用python进行机器学习,感兴趣的朋友可以了解下
torchtext在文本数据预处理方面特别强大,但我们要知道ta能做什么、不能做什么,并如何将我们的需求用torchtext实现。虽然torchtext是为pytorch而设计的,但是也可以与keras、tensorflow等结合使用。
如何用python实现自动翻译工具?很多朋友应该都有遇到过,在查找资料的时候,一些资料都是有大量英文的,那么这对于英文不是很好地朋友来说,阅读就比较困难,因此自动翻译攻击就显得很重要,对这这篇文章就给大家来分析Python 实现自动化翻译和替换的脚本,感兴趣的朋友就接着看吧。
对于Python新手来说,理解python内存分配机制是很有必要的。本文就给大家分享一下实例帮助大家更好的理解python内存分配机制,下面就跟随小编一起来看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008