python之滑动认证(图片)

时间:2021-08-08 15:19:28
from PIL import Image, ImageEnhance
from io import BytesIO def cutImg(imgsrc):
"""
根据坐标位置剪切图片
:param imgsrc: 原始图片路径(str)
:param out_img_name: 剪切输出图片路径(str)
:param coordinate: 原始图片上的坐标(tuple) egg:(x, y, w, h) ---> x,y为矩形左上角坐标, w,h为右下角坐标
:return: from PIL import Image
from PIL import ImageEnhance
#原始图像
image = Image.open('lena.jpg')
image.show()
#亮度增强
enh_bri = ImageEnhance.Brightness(image)
brightness = 1.5
image_brightened = enh_bri.enhance(brightness)
image_brightened.show()
#色度增强
enh_col = ImageEnhance.Color(image)
color = 1.5
image_colored = enh_col.enhance(color)
image_colored.show()
#对比度增强
enh_con = ImageEnhance.Contrast(image)
contrast = 1.5
image_contrasted = enh_con.enhance(contrast)
image_contrasted.show()
#锐度增强
enh_sha = ImageEnhance.Sharpness(image)
sharpness = 3.0
image_sharped = enh_sha.enhance(sharpness)
image_sharped.show() """ # image.save(buffered, format="PNG")
# img_str = base64.b64encode(buffered.getvalue()) x=random.randint(100,260)
y=random.randint(0,80)
w=x+40
h=y+40
coordinate=(x,y,w,h) image = Image.open(imgsrc)
region = image.crop(coordinate)
region = ImageEnhance.Contrast(region).enhance(1.0)
# region2 = ImageEnhance.Contrast(region).enhance(0.1)
region2 = ImageEnhance.Brightness(region).enhance(0.5)
region2.show()
buffered = BytesIO()
region.save(buffered, format="PNG")
img_paste = base64.b64encode(buffered.getvalue()).decode()
buffered.close() image.paste(region2, (x, y))
buffered2 = BytesIO()
image.save(buffered2, format="PNG")
img_bg=base64.b64encode(buffered2.getvalue()).decode()
buffered2.close()

封装成方法

import os
from PIL import Image, ImageEnhance
from io import BytesIO
import base64
import random
import uuid def isint(self, *args):
for nb in args:
try:
int(nb)
except Exception as e:
return (False, nb)
return (True, '') # redis key值开头
prefix_str = "phoebe_auth_" imgpath = os.path.join(os.path.dirname(__file__), '..', 'silder_img') #获取滑动认证的图片
def getAuthImage(redis_conn,uid):
if os.path.isdir(imgpath) is False:
return (False,'{} 不存在'.format(imgpath)) img_list = os.listdir(imgpath)
if img_list:
random_img = img_list[random.randint(0, len(img_list) - 1)]
imgscr = os.path.join(imgpath, random_img)
else:
return (False, '{} 不存在'.format(imgpath)) image = Image.open(imgscr)
width = image.size[0]
height = image.size[1]
if width != 300 or height != 110:
return (False,'图片尺寸:300/110'.format(imgscr))
x = random.randint(100, 260)
y = random.randint(0, 70)
w = x + 40
h = y + 40
coordinate = (x, y, w, h) region = image.crop(coordinate)
region = ImageEnhance.Contrast(region).enhance(1.0)
# region2 = ImageEnhance.Contrast(region).enhance(0.1)
region2 = ImageEnhance.Brightness(region).enhance(0.5)
buffered = BytesIO()
region.save(buffered, format="PNG")
img_paste = base64.b64encode(buffered.getvalue()).decode()
buffered.close() image.paste(region2, (x, y))
buffered2 = BytesIO()
image.save(buffered2, format="PNG")
img_bg = base64.b64encode(buffered2.getvalue()).decode()
buffered2.close() redis = redis_conn # 背景图片
redis.set("%s_%s_bg_img" % (prefix_str, uid), img_bg, 30)
# 可移动图片
redis.set("%s_%s_move_img" % (prefix_str, uid), img_paste, 30)
# 可移动图片x,y坐标
redis.set("%s_%s_move_xy" % (prefix_str, uid), '%s,%s' % (x, y), 30)
# 认证失败次数
redis.set("%s_%s_img_error_count" % (prefix_str, uid), 0, 30) data = {
'bg_img': img_bg,
'move_img': img_paste,
'move_y': y
}
return (True,data) #认证图片是否移动到指定位置
def AuthImage(redis_conn, uid,move_x,move_y):
isint_ret = isint(move_x, move_y)
if isint_ret[0] is False:
describe = 'The "%s" data type is int' % (isint_ret[1])
return (False,describe) # 获取x,y坐标
redis=redis_conn r_xy = redis.get("%s_%s_move_xy" % (prefix_str, uid))
if r_xy:
r_xy_list = r_xy.decode().split(',')
r_x = r_xy_list[0]
r_y = r_xy_list[1]
if abs(int(r_x) - int(move_x)) <= 1 and abs(int(r_y) - int(move_y)) <= 1:
redis.delete("%s_%s_bg_img" % (prefix_str, uid))
redis.delete("%s_%s_move_img" % (prefix_str, uid))
redis.delete("%s_%s_move_xy" % (prefix_str, uid))
redis.delete("%s_%s_img_error_count" % (prefix_str, uid))
random_str = str(uuid.uuid1()).replace('-', '')
redis.set("%s_%s_img_randm_str" % (prefix_str, uid), random_str, 30)
return (True,random_str)
else:
error_count = redis.get("%s_%s_img_error_count" % (prefix_str, uid))
if error_count:
error_count = int(error_count.decode())
new_count = error_count + 1
if new_count > 5:
describe = '%s 尝试次数过多' % (uid)
redis.delete("%s_%s_bg_img" % (prefix_str, uid))
redis.delete("%s_%s_move_img" % (prefix_str, uid))
redis.delete("%s_%s_move_xy" % (prefix_str, uid))
redis.delete("%s_%s_img_error_count" % (prefix_str, uid))
return (False,describe,True)
else:
redis.set("%s_%s_img_error_count" % (prefix_str, uid), new_count, 30)
describe = '%s 认证失败' % (uid)
return (False,describe)
else:
describe = '%s 认证失败' % (uid)
return (False,describe,True)
else:
describe = '%s 认证失败' % (uid)
return (False,describe,True)