滑块拼图验证码识别

通常滑块验证码都是横向滑动,今天看到一个比较特别的滑块拼图验证码,他不仅能在横向上滑动,还需要进行纵向滑动。如下图所示:

他的滑块在背景图片的左上角,需要鼠标拖动左上角的滑块,移动到背景图的缺口位置拼好。

首先会识别滑块左上角的坐标,然后识别缺口左上角的坐标。然后他们两个相减,就是x,y方向上的滑动距离

识别代码如下:

import base64
import requests
import datetime
from io import BytesIO
from PIL import Image, ImageDraw

t1 = datetime.datetime.now()

#PIL图片保存为base64编码
def PIL_base64(img, coding='utf-8'):
    img_format = img.format
    if img_format == None:
        img_format = 'JPEG'

    format_str = 'JPEG'
    if 'png' == img_format.lower():
        format_str = 'PNG'
    if 'gif' == img_format.lower():
        format_str = 'gif'

    if img.mode == "P":
        img = img.convert('RGB')
    if img.mode == "RGBA":
        format_str = 'PNG'
        img_format = 'PNG'

    output_buffer = BytesIO()
    # img.save(output_buffer, format=format_str)
    img.save(output_buffer, quality=100, format=format_str)
    byte_data = output_buffer.getvalue()
    base64_str = 'data:image/' + img_format.lower() + ';base64,' + base64.b64encode(byte_data).decode(coding)
    # base64_str = base64.b64encode(byte_data).decode(coding)

    return base64_str

# 加载图片
img1 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\53-1.png')
# 图片转base64
img1_base64 = PIL_base64(img1)

# 验证码识别接口
url = "http://www.detayun.cn/openapi/verify_code_identify/"
data = {
    # 用户的key
    "key":"78t9jTqCbAnHFO0tbBDW",
    # 验证码类型
    "verify_idf_id":"53",
    # 样例图片
    "img_base64":img1_base64,
}
header = {"Content-Type": "application/json"}

# 发送请求调用接口
response = requests.post(url=url, json=data, headers=header)

# 获取响应数据,识别结果
print(response.text)
print("耗时:", datetime.datetime.now() - t1)

# 获取识别距离
point = eval(response.json()['data']['res_str'])
img1 = img1.convert("RGB")
draw = ImageDraw.Draw(img1)
x = point[0]
y = point[1]
draw.line((x, 0, x, img1.size[1]), fill=(255, 0, 0), width=2)  # 线的起点和终点,线宽
draw.line((0, y, img1.size[0], y), fill=(255, 0, 0), width=2)  # 线的起点和终点,线宽
img1.show()

想了解更多验证码识别,请访问:得塔云

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dxy1239310216

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值