当前位置: 首页 > article >正文

京东图标点选验证码识别代码

 

如上图所示,京东会让你根据小图中的图案,在大图中点击对应的图标。

识别代码如下:

需要两张图片,可以是原图2张,或者是截图2张。运行下面代码可以看到点击的位置。

import base64
import requests
import datetime
import numpy as np
from io import BytesIO
from PIL import Image,ImageDraw,ImageFont

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)

    return base64_str

# 旋转图片
def rotate_img(img, angle):
    # 转换为有alpha层
    temp_img2 = img.convert('RGBA')
    # 旋转
    rot = temp_img2.rotate(-angle)
    # 创建一个与旋转图像大小相同的白色图像
    fff = Image.new('RGBA', rot.size, (255, 255, 255, 0))
    # 使用alpha层的rot作为掩码创建一个复合图像
    out = Image.composite(rot, fff, rot)
    # 将临时图片转换为元素图片颜色模式
    temp_img2 = out.convert(img.mode)

    return temp_img2

# 加载外圈大图
img1 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\59-1.jpg')
# 图片转base64
img1_base64 = PIL_base64(img1)
# 加载内圈小图
img2 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\59-2.jpg')
# 图片转base64
img2_base64 = PIL_base64(img2)

# 验证码识别接口
url = "https://www.detayun.cn/openapi/verify_code_identify/"
data = {
    # 用户的key
    "key":"EI4vT8UGFTXRE4hZR7cE",
    # 验证码类型
    "verify_idf_id":"59",
    # 外圈大图
    "img1":img1_base64,
    # 内圈小图
    "img2":img2_base64,
}
header = {"Content-Type": "application/json"}
# 发送请求调用接口
response = requests.post(url=url, json=data, headers=header)

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

# 标记识别结果
draw = ImageDraw.Draw(img1)
# 字体设置
font_type = "./msyhl.ttc"
font_size = 20
font = ImageFont.truetype(font_type, font_size)
# 获取结果列表
y = response.json()['data']['res_str']
point_list = eval(y)
# 标记点击序号
for i, point in enumerate(point_list):
    draw.ellipse((point[0] - 15, point[1] - 15,point[0] + 15, point[1] + 15), fill=(255, 0, 0))
    draw.text((point[0] - 5, point[1] - 15), str(i + 1), fill=(255, 255, 255), font=font)

img1.show()

识别效果图

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


http://www.kler.cn/a/297809.html

相关文章:

  • 【学习笔记】手写 Tomcat -- 预备知识
  • python产生8位随机整数
  • MySQL数据库SQL语句和常用函数大全
  • 力扣-9. 回文数
  • 企业如何治理“两高一弱”?
  • 【MySQL】MySQL表的操作
  • 如何编写Prompt,利用AI高效生成图表——图表狐(FoxChart)指南
  • Git 基础操作手册:轻松掌握常用命令
  • 【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)
  • 如何在Vue实例上挂载自己定义的工具类
  • 常用 Excel 笔记
  • 攻防世界 ics-05
  • 用亚马逊云科技Graviton高性能/低耗能处理器构建AI向量数据库(上篇)
  • 开源模型应用落地-sherpa-onnx-AIGC应用探索(十)
  • flutter的入口和原生交互
  • 【深度学习】【onnxruntime】C++调用onnx
  • TinyWebSever源码逐行注释(三)_ thread_pool.cpp
  • 动手学深度学习(pytorch)学习记录25-汇聚层(池化层)[学习记录]
  • 等保2.0测评之Nginx 中间件
  • 操作word 以及pdf 记录以及踩坑总结