def get_position_table(): """获取指定列的数据""" # 获取焦点 get_focus() # 功能键F1,转到买入选项卡 keyboard_input(function_key='F4') log.info('获取程序焦点,调出股票持仓查询页面') # 第一步是先获取屏幕截图 pic=get_screenshot() log.info('获取屏幕截图') # 获取表格下边框,防止搜索过界 info = config.pic_info['持仓_汇总'] bin = info['bin'] xrange = info['xrange'] yrange = info['yrange'] # 获取子图 file = os.path.join(config.gvariable.work_path, 'pic', '持仓_汇总.png') print(file) im = read_im(file, threshold=bin) # 因为已经是二值图,这里的阈值就没必要了 im = cut_blank(im) # 获取主图的二值图 pic2 = to_bin_img(pic.copy(), threshold=bin) # 接下来就是找到边界了 maxdown,y=find_location(pic2,im,xrange,yrange,'leftup') log.info('获取最大搜索下边界 %d'%maxdown) # 开始循环获取数字串 cols = ['持仓_证券代码', '持仓_股票余额', '持仓_可用余额', '持仓_冻结数量', '持仓_成本价', '持仓_市价', '持仓_市值'] # col=cols[0] tables=[] for col in cols: # 获取准备好的子图信息 info=config.pic_info[col] bin=info['bin'] xrange=info['xrange'] yrange=info['yrange'] # 获取子图 file=os.path.join(config.gvariable.work_path,'pic',col+'.png') im=read_im(file,threshold=bin) # 因为已经是二值图,这里的阈值就没必要了 im=cut_blank(im) # 获取主图的二值图 pic2=to_bin_img(pic.copy(),threshold=bin) # 接下来就是获取列的信息了 col_info=get_cols_pic_info(pic2,im,xrange,yrange,maxdown) tables.append(col_info) log.info('完成 %s 数据解析'%col) # 转成dataframe position=pd.DataFrame(tables,index=cols).T log.info('完成表格所有列解析\n'+str(position)) return position
def max_apps_windows(): """判断当前是否最小化,如果是,则点击任务栏图标,实现最大化""" # 找到最大化窗口图标位置 im, bin, xrange, yrange = read_im_info('界面_已最大化窗口') # 截图,判断匹配位置 pic = get_screenshot() pic2 = to_bin_img(pic.copy(), bin) x, y = find_location(pic2, im, xrange, yrange, 'center') # 判断是否已经最大化 if x > 0 or y > 0: log.info('窗口已经最大化,无需点击') return 0 # 没有最大化,找到点击窗口图标 log.info('窗口未最大化,需要最大化窗口') im, bin, xrange, yrange = read_im_info('界面_需最大化窗口') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x, y = find_location(pic2, im, xrange, yrange, 'center') if x == -1 or y == -1: log.info('没有找到最大化窗口图标,请检查是否最小化或者锁频了') return -1 else: mouse_click(x, y) log.info('点击窗口最大化 %d %d ,解锁交易界面' % (x, y))
def unlock(passwd='123456'): """判断是否需要解锁界面""" # 找到锁屏标志 im, bin, xrange, yrange = read_im_info('界面_解锁密码') # 截图,判断匹配位置 pic = get_screenshot() pic2 = to_bin_img(pic.copy(), bin) x, y = find_location(pic2, im, xrange, yrange, 'center') # 判断是否锁屏 if x == -1 or y == -1: log.info('交易界面没有锁定,无需解锁') else: log.info('交易界面锁定,需要解锁') mouse_click(x, y) keyboard_input(passwd) log.info('移动到 %d %d ,输入解锁密码' % (x, y)) # 找到确认按钮 im, bin, xrange, yrange = read_im_info('界面_解锁确认') im = cut_blank(im) x1 = (x - 100) / pic.shape[0] if x - 100 >= 0 else 0 x2 = (x + 100) / pic.shape[0] if x + 100 <= pic.shape[0] else 1 y1 = (y - 200) / pic.shape[1] if y - 200 >= 0 else 0 y2 = (y + 200) / pic.shape[1] if y + 200 <= pic.shape[1] else 1 xrange, yrange = [x1, x2], [y1, y2] pic2 = to_bin_img(pic.copy(), bin) xc, yc = find_location(pic2, im, xrange, yrange, 'center') mouse_click(xc, yc) log.info('点击确认按钮 %d %d ,解锁交易界面' % (xc, yc)) return
def test(): # # 读入全图 file = r"E:\python_code\ocrstock\pic\全图.png" # file=r"F:\wo\python_code\ocrstock\pic\全图.png" pic = read_im(file, threshold=200) plt.imshow(pic) # 找到证券代码位置 file = r"E:\python_code\ocrstock\pic\持仓_证券代码.png" # file=r"F:\wo\python_code\ocrstock\pic\证券代码.png" im = read_im(file, threshold=100) im = cut_blank(im, 0, 0) plt.imshow(im) get_position_table()
def get_coordinate(pic, im_name): """获取指定子图的坐标""" # 读取子图,及其config数据 file = os.path.join(config.gvariable.picpath, im_name + '.png') info = config.pic_info[im_name] bin = info['bin'] xrange = info['xrange'] yrange = info['yrange'] # 读取子图,转成二值图 im = read_im(file, info['bin']) im = cut_blank(im) # 主图也转成二值图 pic2 = to_bin_img(pic, threshold=bin) # 找到四个角坐标 x1, y1 = find_location(pic2, im, xrange, yrange, 'leftup') x2, y2 = x1 + im.shape[0], y1 + im.shape[1] return x1, x2, y1, y2
def rec_nums(im): """识别图片中的数字串""" # 导入训练好的模型 # m = joblib.load(model) # 删除图片左右两边和上下两边的空白 im = cut_pic.cut_blank(im) # 切分成单个数字 words = cut_pic.split_num(im) # resize words = [cut_pic.resize_pic(p, 10, 15) for p in words] # 转成矩阵,摊平处理 words = [p.ravel().reshape(1, -1) for p in words] # 下面是识别程序了 nums = [m.predict(p)[0] for p in words] to_str = lambda x: str(x) if x < 10 else '.' nums = ''.join([to_str(x) for x in nums]) return nums
def create_sample(): """ 将上面的流程串起来,批量生成测试图片 :return: """ # 测试的字体 fonts=win_fonts # 测试的字号变化 pixs=list(range(15,25)) # 循环测试 id=0 for font in fonts: for pix in pixs: for _ in range(10): # 生成随机字符串 string = create_random_num() # 生成对应的图片数组, # 删除周边空白 # 切割图片 # resize图片 im = create_pic(string, pix=pix, font_path=font) im = cut_blank(im, xleft=0, yleft=0) nums1 = split_num(im) x,y=resize_dim nums2 = [resize_pic(p, x, y) for p in nums1] # 保存图片,之所以搞这么多余,是因为在宋体下,有可能两个数字粘连导致无法切分出错 if len(nums1)==len(string): for i in range(len(nums1)): name = '10' if string[i] == '.' else string[i] # 保存原图 file1 = os.path.join(pict_path, '%s_%d_old.png' % (name, id)) Image.fromarray(nums1[i]).save(file1) # 保存resize图 file2 = os.path.join(pict_path, '%s_%d_resize.png' % (name, id)) Image.fromarray(nums2[i]).save(file2) # id自增 id+=1 # 打印信息 t=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print('%s pix=%2d string=%s font=%s'%(t,pix,string,os.path.split(font)[-1])) # return
def create_sample(): """ 将上面的流程串起来,批量生成测试图片 :return: """ # 测试的字体 fonts = win_fonts # 测试的字号变化 pixs = list(range(15, 25)) # 循环测试 id = 0 for font in fonts: for pix in pixs: for _ in range(10): # 生成随机字符串 string = create_random_num() # 生成对应的图片数组, # 删除周边空白 # 切割图片 # resize图片 im = create_pic(string, pix=pix, font_path=font) im = cut_blank(im, xleft=0, yleft=0) x, y = resize_dim im2 = resize_pic(im, x, y) # 保存原图 file1 = os.path.join(pict_path, '%s_%d_old.png' % (string, id)) Image.fromarray(im).save(file1) # 保存resize图 file2 = os.path.join(pict_path, '%s_%d_resize.png' % (string, id)) Image.fromarray(im2).save(file2) # id自增 id += 1 # 打印信息 t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print('%s pix=%2d string=%s font=%s' % (t, pix, string, os.path.split(font)[-1])) # return
def buy(symbol, price, amount): """买入股票""" # 获取焦点 get_focus() # 功能键F1,转到买入选项卡 keyboard_input(function_key='F1') log.info('获取程序焦点,调出股票买入页面') # 截图 pic = get_screenshot() # 获取 买入股票 标志坐标 im, bin, xrange, yrange = read_im_info('买入_买入页') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), threshold=bin) x, y = find_location(pic2, im, xrange, yrange, 'leftup') log.info('买入股票 坐标 %d %d ' % (x, y)) # 计算其他几个标志的查找范围 high = pic.shape[0] * 0.3 width = pic.shape[1] * 0.3 x1 = (x - 10) / pic.shape[0] if x - 10 >= 0 else 0 x2 = (x + high) / pic.shape[0] if x + high <= pic.shape[0] else 1 y1 = (y - 10) / pic.shape[1] if y - 10 > 0 else 0 y2 = (y + width) / pic.shape[1] if y + width <= pic.shape[1] else 1 # 获取 证券代码,买入价格,买入数量,买入 # 证券代码 im, bin, xrange, yrange = read_im_info('买入_证券代码') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_zqdm, y_zqdm = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('证券代码 坐标 %d %d ' % (x_zqdm, y_zqdm)) # 买入价格 im, bin, xrange, yrange = read_im_info('买入_买入价格') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_mrjg, y_mrjg = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('买入价格 坐标 %d %d ' % (x_mrjg, y_mrjg)) # 买入数量 im, bin, xrange, yrange = read_im_info('买入_买入数量') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_mrsl, y_mrsl = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('买入数量 坐标 %d %d ' % (x_mrsl, y_mrsl)) # 买入确认 im, bin, xrange, yrange = read_im_info('买入_买入确认') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_mrqr, y_mrqr = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('买入确认 坐标 %d %d ' % (x_mrqr, y_mrqr)) # 重填 im, bin, xrange, yrange = read_im_info('买入_重填') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_ct, y_ct = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('买入重填 坐标 %d %d ' % (x_ct, y_ct)) # 下面买入股票 # 重置所有输入 mouse_click(x_ct, y_ct) # 输入代码 mouse_click(x_zqdm, y_zqdm) keyboard_input(symbol) # 输入价格 mouse_click(x_mrjg, y_mrjg) keyboard_input(str(price)) # 输入数量 mouse_click(x_mrsl, y_mrsl) keyboard_input(str(amount)) # 点击确认 mouse_click(x_mrqr, y_mrqr) log.info('买入 symbol=%s price=%f amount=%d' % (symbol, price, amount)) # 后面还有弹出确认框的确认 log.info('后面还有弹出确认框的确认, 未处理')