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

 

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

识别代码如下:

需要两张图片,可以是原图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()

识别效果图

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值