def smallNeedleVsLargeNeedle(): # This test shows that larger needle images take longer to find. However, for small-ish needles (100x100 and smaller) it doesn't really matter. largeNoiseFp = open('largenoise.png' ,'rb') largeNoiseIm = Image.open(largeNoiseFp) imageWidth, imageHeight = largeNoiseIm.size needle10x10 = largeNoiseIm.crop((imageWidth - 10, imageHeight - 10, imageWidth, imageHeight)) needle100x100 = largeNoiseIm.crop((imageWidth - 100, imageHeight - 100, imageWidth, imageHeight)) needle500x500 = largeNoiseIm.crop((imageWidth - 500, imageHeight - 500, imageWidth, imageHeight)) profiler = cProfile.Profile() profiler.enable() list(pyscreeze.locateAll(needle10x10, largeNoiseIm)) profiler.disable() print('10x10 needle:') pstats.Stats(profiler).sort_stats('cumulative').print_stats(1) profiler = cProfile.Profile() profiler.enable() list(pyscreeze.locateAll(needle100x100, largeNoiseIm)) profiler.disable() print('100x100 needle:') pstats.Stats(profiler).sort_stats('cumulative').print_stats(1) profiler = cProfile.Profile() profiler.enable() list(pyscreeze.locateAll(needle500x500, largeNoiseIm)) profiler.disable() print('500x500 needle:') pstats.Stats(profiler).sort_stats('cumulative').print_stats(1)
def ocr_2(box, font_library_path, show=False, threshold=[160, 190], confidence=0.8, pic_path=None): if pic_path: pic = Image.open(pic_path) else: pic = pyscreeze.screenshot(region=box) pic = convert_2(pic, threshold) if show: pic.show() list = [] for filename in os.listdir(font_library_path): points = tuple( pyscreeze.locateAll(font_library_path + filename, pic, confidence=confidence)) if len(points): s = filename[:filename.rindex('.')] for point in points: list.append((point[0], s)) list.sort() s = '' for n in list: s += str(n[1]) # num = None try: # num = int(s) pic.fp.close() except Exception: pass return s
def ocr(box, font_library_path, show=False): pic = pyscreeze.screenshot(region=box) if show: pic.show() list = [] # todo for filename in os.listdir(font_library_path): points = tuple( pyscreeze.locateAll(font_library_path + filename, pic, confidence=0.97)) if len(points): for point in points: list.append((point[0], filename[:filename.rindex('.')])) list.sort() s = '' for n in list: s += str(n[1]) # num = None try: # num = int(s) pic.fp.close() except Exception: pass return s
def test_locateAll_im(self): slashFp = open('slash.png', 'rb') haystack1Fp = open('haystack1.png', 'rb') haystack2Fp = open('haystack2.png', 'rb') colorNoiseFp = open('colornoise.png', 'rb') slashIm = Image.open(slashFp) haystack1Im = Image.open(haystack1Fp) haystack2Im = Image.open(haystack2Fp) colorNoiseIm = Image.open(colorNoiseFp) self.assertEqual(((94, 94, 4, 4), ), tuple(pyscreeze.locateAll(slashIm, haystack1Im))) self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll(slashIm, haystack2Im))) self.assertEqual( ((94, 94, 4, 4), ), tuple(pyscreeze.locateAll(slashIm, haystack1Im, grayscale=True))) self.assertEqual( ((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll(slashIm, haystack2Im, grayscale=True))) self.assertEqual((), tuple(pyscreeze.locateAll(slashIm, colorNoiseIm))) self.assertEqual( (), tuple(pyscreeze.locateAll(slashIm, colorNoiseIm, grayscale=True))) slashFp.close() haystack1Fp.close() haystack2Fp.close() colorNoiseFp.close()
def test_locateAll_filename(self): self.assertEqual( ((94, 94, 4, 4), ), tuple(pyscreeze.locateAll('slash.png', 'haystack1.png'))) self.assertEqual( ((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll('slash.png', 'haystack2.png'))) self.assertEqual(((94, 94, 4, 4), ), tuple( pyscreeze.locateAll('slash.png', 'haystack1.png', grayscale=True))) self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple( pyscreeze.locateAll('slash.png', 'haystack2.png', grayscale=True))) self.assertEqual( (), tuple(pyscreeze.locateAll('slash.png', 'colornoise.png'))) self.assertEqual((), tuple( pyscreeze.locateAll('slash.png', 'colornoise.png', grayscale=True)))
def test_locateAll_filename(self): self.assertEqual(((94, 94, 4, 4),), tuple(pyscreeze.locateAll('slash.png', 'haystack1.png'))) self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll('slash.png', 'haystack2.png'))) self.assertEqual(((94, 94, 4, 4),), tuple(pyscreeze.locateAll('slash.png', 'haystack1.png', grayscale=True))) self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll('slash.png', 'haystack2.png', grayscale=True))) self.assertEqual((), tuple(pyscreeze.locateAll('slash.png', 'colornoise.png'))) self.assertEqual((), tuple(pyscreeze.locateAll('slash.png', 'colornoise.png', grayscale=True)))
def local_all(self, source_img): """ 返回所有识别准确的图片,的横坐标和纵坐标 :return: [(int(x1), int(y1)), (int(x2), int(y2))...] """ all_result = pyscreeze.locateAll(self.find_img, source_img, confidence=self.confidence) print(self.find_img) print(all_result) if all_result: # 结构化数据,返回结果集[(int(x1), int(y1)), (int(x2), int(y2))... element_positions = [] for x in all_result: element_positions.append(tuple(map(int, x['result']))) return element_positions raise FindImgError('无法识别图片')
def locate_all_on_screen(self, image, **kwargs): """ 在屏幕中定位指定图片的所有位置 @param {str|PIL.Image} image - 要定位的图片文件路径或图片对象 @param {dict} kwargs - 其他执行参数: {bool} grayscale=False - 是否转换图片为灰度检索(提升30%速度) {int} limit=10000 - 匹配数量限制 {int} step=1 - 匹配步骤,支持送1或2,如果为2会跳过细节,大约能提升3倍速度,但有可能匹配不上 {float} confidence=0.999 - 匹配度 @returns {list} - 返回所找到的所有图片的位置 [(x, y, width, height), ..] """ if type(image) == str: _image = Image.open(image) else: _image = image screenshotIm = self.screenshot() retVal = pyscreeze.locateAll(_image, screenshotIm, **kwargs) return list(retVal)
def test_locateAll_im(self): slashFp = open('slash.png' ,'rb') haystack1Fp = open('haystack1.png' ,'rb') haystack2Fp = open('haystack2.png' ,'rb') colorNoiseFp = open('colornoise.png' ,'rb') slashIm = Image.open(slashFp) haystack1Im = Image.open(haystack1Fp) haystack2Im = Image.open(haystack2Fp) colorNoiseIm = Image.open(colorNoiseFp) self.assertEqual(((94, 94, 4, 4),), tuple(pyscreeze.locateAll(slashIm, haystack1Im))) self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll(slashIm, haystack2Im))) self.assertEqual(((94, 94, 4, 4),), tuple(pyscreeze.locateAll(slashIm, haystack1Im, grayscale=True))) self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll(slashIm, haystack2Im, grayscale=True))) self.assertEqual((), tuple(pyscreeze.locateAll(slashIm, colorNoiseIm))) self.assertEqual((), tuple(pyscreeze.locateAll(slashIm, colorNoiseIm, grayscale=True))) slashFp.close() haystack1Fp.close() haystack2Fp.close() colorNoiseFp.close()
def find_pic(self, target_pic): last_window = self.window_history[-1] result = list(pyscreeze.locateAll(target_pic, last_window)) self.app.send({'status': VIEWER_FINDED, 'data': result})