赞
踩
post_pic
用于传验证码字节流,和验证码类型,然后获得识别结果;resport_error传图片id上报错误的图片识别,不扣分#!/usr/bin/env python # coding:utf-8 import requests from hashlib import md5 from PIL import Image, ImageDraw, ImageFont from io import BytesIO import cv2 class CjyClient: def __init__(self, username, password, soft_id): self.username = username self.password = md5(password.encode('utf8')).hexdigest() self.soft_id = soft_id self.base_params = { 'user': self.username, 'pass2': self.password, 'softid': self.soft_id, } self.headers = { 'Connection': 'Keep-Alive', 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)', } def post_pic(self, im, code_type): """ im: 图片字节 code_type: 题目类型 参考 http://www.chaojiying.com/price.html 9101 坐标选一,返回格式:x,y 15 9004 坐标多选,返回1~4个坐标,如:x1,y1|x2,y2|x3,y3 25 默认超级鹰识别结果为小写,如果要区分大小写,则文字提示和图片通过程序合成一张图片再上传(点选也是) """ params = {'codetype': code_type} params.update(self.base_params) files = {'userfile': ('ccc.jpg', im)} r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers) return r.json() def report_error(self, im_id): """ im_id:报错题目的图片ID {"err_no":0,"err_str":"OK","pic_id":"1176318810001","pic_str":"241,108|178,62|69,45","md5":"5fd97b61393d02144f019"} """ params = {'id': im_id} params.update(self.base_params) r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers) return r.json() class ImageProcess: @staticmethod def slice_img(img_content, save_path="bg.png", words=None): """ 合并图片,在底部加入文字提示图片 Image.new(mode, size, color): 这个方法可以生成一张图片,有三个参数, mode:颜色空间模式,可以是'RGBA','RGB','L'等等模式, size:图片尺寸,接收一个两个整数的元组, color:图片的填充颜色,可以是red,green等,也可以是rgb的三个整数的元组。也就是背景颜色 :param img_content: 传入图片字节流 :param save_path: 图片保存路径 :param words: 文字提示 :return: 返回图片字节流 """ img_bg = Image.open(BytesIO(img_content)) width, height = img_bg.size bg_img = Image.new("RGB", (width, height+30), (255, 255, 255)) bg_img.paste(img_bg, box=(0, 0, width, height)) if words: # 文字填充图片 font_im = Image.new("RGB", (width, 30), (255, 255, 255)) dr = ImageDraw.Draw(font_im) font = ImageFont.truetype("simsun.ttc", 12, encoding='utf-8') dr.text((10, 5), words, font=font, fill="#000000") bg_img.paste(font_im, box=(0, height, width, height+30)) # 获得图片字节流 bg_img.save(save_path) bytes_img_io = BytesIO() bg_img.save(bytes_img_io, format='PNG') return bytes_img_io.getvalue() @staticmethod def mark_img(path, xy_list): """在图片上标记坐标点""" image = cv2.imread(path) for xy in xy_list: image = cv2.circle(image, (xy[0], xy[1]), radius=3, color=(0, 0, 255), thickness=-1) cv2.imwrite('./label.png', image) @staticmethod def tran_xy(xy_list_str): """ xy坐标转换 :param xy_list_str: "241,108|178,62|69,45" :return: [[241, 108], [178, 62], [69, 45]] """ xy_lis = [[int(t) for t in xy.split(",")] for xy in xy_list_str.split("|")] return xy_lis if __name__ == '__main__': Cjy = CjyClient('syy_name', 'w_pwd', '12323123') # 用户名, 密码, 软件ID(用户中心>>软件ID 生成一个替换 12323123) Cjy.post_pic(open('out.png', 'rb').read(), 9004) # 9004代表验证码类型 # ImageProcess.slice_img(open('img.png', 'rb').read(), words="请依次点击:哈哈哈") # ImageProcess.mark_img('img.png', [(127, 54), (69, 65), (85, 112)]) # print(ImageProcess.tran_xy("241,108|178,62|69,45"))
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。