#!/usr/bin/env python
# -*- coding:utf-8 -*-
from PIL import Image, ImageFilter
import random
import glob
# from matplotlib.pyplot import imsave
import base64
from io import BytesIO
import redis,os
backgroundPaton = os.getcwd()+"/app/codeimg/*.[pP][nN][gG]"
maskPaton = os.getcwd()+"/app/codeimgMask/*[0-9]-[bB].[pP][nN][gG]"
yMargin = 2
kSuffix = "_captchaCode"
class captcha:
def __init__(self, rHost='127.0.0.1', rPort=6379, rDb=1, rPasswd='123456'):
self.host = rHost
self.port = rPort
self.db = rDb
self.passwd = rPasswd
# self.r = rds
def getRedis(self):
pool = redis.ConnectionPool(host=self.host, port=self.port, db=self.db, password=self.passwd)
return redis.StrictRedis(connection_pool=pool)
# return redis.Redis(host=self.host, port=self.port,db=self.db,password=self.passwd)
def getCode(self, did):
if (len(did) < 32):
return 0, 0
bImgFile = random.choice(glob.glob(backgroundPaton)) # 随机取底图文件
bBorderFile = random.choice(glob.glob(maskPaton)) # 随机取底图蒙层文件
# print(bBorderFile)
fBorderPaton = bBorderFile[:(len(bBorderFile) - 5)] + "[wW].[pP][nN][gG]" #
fBorderFile = random.choice(glob.glob(fBorderPaton)) # 上层图蒙层文件
# print(fBorderFile)
bImg = Image.open(bImgFile) # 底图
bBorder = Image.open(bBorderFile) # 底图蒙层
fBorder = Image.open(fBorderFile) # 上层图蒙层
xPos = random.randint(bBorder.size[0], bImg.size[0] - bBorder.size[0]) # 取图x坐标
yPos = random.randint(yMargin, bImg.size[1] - bBorder.size[1] - yMargin) # 取图y坐标
pos = (xPos, yPos)
reg = pos + (xPos + bBorder.size[0], yPos + bBorder.size[1]) # 取图矩形区域
cImg = bImg.crop(reg) # 底图中取矩形图
bMask = fBorder.convert('RGBA') # 蒙层边界
fChip = Image.composite(cImg, bMask, mask=fBorder) # 取出图片合成外层边框
fImg = Image.new("RGBA", bImg.size)
fImg.paste(fChip, (0, yPos))
#fImg.save("front.png")
fImgBuf = BytesIO()
fImg.save(fImgBuf, format="png")
bMask = bBorder.convert('RGBA') # 蒙层边界
bImg.paste(bBorder, pos, mask=bMask) # 叠加蒙层
#bImg.save("back.png")
bImgBuf = BytesIO()
bImg.save(bImgBuf, format="png")
rds = self.getRedis()
rds.set(did + kSuffix, xPos, ex=60)
self.fImgStream = 'data:image/png;base64,'+base64.b64encode(fImgBuf.getbuffer()).decode()
self.bImgStream = 'data:image/png;base64,'+base64.b64encode(bImgBuf.getbuffer()).decode()
# return self.fImgStream,self.bImgStream
return {"code":200,"frontImg": self.fImgStream, "backGoundImg": self.bImgStream}
#获取通过码状态
def getCodeState(self,did):
rdsKeyResult = did + "_captchaResult"
rds = self.getRedis()
xValue=False
try:
xValue = rds.get(rdsKeyResult)
xValue=bool(xValue)
rds.delete(rdsKeyResult)
except:
print("获取失败")
return xValue
def chkCode(self, did, xPos, off=2): # 1-验证成功;2-验证失败;3-验证码过期;4-验证次数超过3次,off-误差值
rdsKey = did + kSuffix
rdsChkKey = did + "_captchaCnt"
rdsKeyResult = did + "_captchaResult"
rds = self.getRedis()
try:
xValue = rds.get(rdsKey)
xPosRDS = int(xValue)
except:
rds.delete(rdsKey)
rds.delete(rdsChkKey)
return 3
rdsChkCount = rds.incr(rdsChkKey)
if (rdsChkCount > 3):
rds.delete(rdsKey)
rds.delete(rdsChkKey)
return 4
if (abs(xPosRDS - xPos) <= off):
rds.delete(rdsKey)
rds.delete(rdsChkKey)
rds.set(rdsKeyResult, True, ex=30)
return 1
else:
return 2
# 生成手机号验证码
def setSmsCode(self, did):
rdsKeyResult = did + "_captchaMobile"
rds = self.getRedis()
randomnum=random.randint(10000,999999)
try:
rds.set(rdsKeyResult, randomnum, ex=120)
result = {"code": 200, "smscode": randomnum}
except:
result = {"code": 203, "smscode":0}
return result
# 检测短信验证码
def chkSmsCode(self, did,smscode):
rdsKeyResult = did + "_captchaMobile"
rds = self.getRedis()
xValue = False
try:
xall = rds.get(rdsKeyResult)
xall = int(xall)
if(int(smscode)==xall):
rds.delete(rdsKeyResult)
xValue=True
except:
print("验证失败")
return xValue
if __name__=='__main__':
myCaptcha = captcha()
print(myCaptcha.getCode('abcdefghijklmnopqrstuvwxdefghi3d'))
#print(myCaptcha.chkCode('abcdefghijklmnopqrstuvwxdefghi3d', 182))
- 1
- 2
前往页