def debug(self, img: Image): img = self.draw(img) img = self.button.draw(img) for s in self.slots: img = s.draw(img) img.show() return img
def test_find_image_exact(self): haystack = Image.open('test/login2.png') needle = Image.open('test/login-slice.png') matches = find.image_exact(haystack, needle) if imgshow: for match in matches: haystack = match.draw(haystack) haystack = draw_text(haystack, "test_find_image_exact", (0, 0)) haystack.show() self.assertEqual(len(matches), 1)
def test_find_image_exact_masked(self): img = Image.open('./scripts/scaperune/images/older.png') mmap = Image.open('./scripts/scaperune/images/minimap-border.png') mmap.mask = mmap == [0, 0, 0] # mask transparent aka black values matches = find.image_exact(img, mmap) print(len(matches)) for match in matches: img = match.draw(img) img = draw_text(img, "test_find_image_exact_masked", (0, 0)) img.show()
def test_find_images(self): # test core/find.imagecv2 haystack = Image.open('test/login2.png') needle = Image.open('test/login-slice.png') matches = find.images(haystack, needle, threshold=0.95) if imgshow: for match in matches: haystack = draw_text(haystack, "test_find_images", (0, 0)) haystack = match.draw(haystack) haystack.show() self.assertEqual(len(matches), 1)
def test_find_image_masked(self): mmap = Image.open('test/minimap-border.png', alpha_mask=True) img = Image.open('test/test-mmap.png') # cv2.TM_CCORR_NORMED didnt work here... match = find.image(img, mmap, method=cv2.TM_SQDIFF) if imgshow: img = draw_text(img, "test_find_image_masked", (0, 0)) img = match.draw(img) img.show() self.assertIsNotNone(match)
def test_find_image(self): # test core/find.image haystack = Image.open('test/login.png') # screenshot of haystack needle = Image.open('test/login-slice.png') match = find.image(haystack, needle) if imgshow: haystack = draw_text(haystack, "test_find_image", (0, 0)) haystack = match.draw(haystack) haystack.show() self.assertIsNotNone(match)
def test_find_map_position(self): from pysrl.scripts.scaperune.interface import minimap map_img = Image.open('test/osrs-world-map.png') screen_img = Image.open('test/older2.png') mmap_img = minimap.get_image(screen_img) match = find.image(map_img, mmap_img, method=cv2.TM_SQDIFF) if imgshow: map_img = draw_text(map_img, "test_find_map_position", (0, 0)) map_img = draw_text(map_img, str(match), (0, 15)) match.draw(map_img).show() self.assertIsNotNone(match) self.assertEqual(match.top_left, Point(4520, 3184))
def test_find_text(self): # test core/find.text img = Image.open('test/login2.png') match = find.text(img, 'New User') if imgshow: img = draw_text(img, "test_find_text", (0, 0)) img = match.draw(img) img.show() self.assertIsNotNone(match)
def test_find_colors(self): # test core/color.py img = Image.open('test/test.jpeg') cts = CTS2([0, 0, 0], 10, 10, 10) pts = find.colors(img, cts) if imgshow: img = draw_text(img, "test_find_colors", (0, 0)) pts.draw(img).show() self.assertEqual(len(pts), 2560)
def test_pa_cluster(self): # test core/types/point_array.py img = Image.open('test/test.jpeg') cts = CTS2([0, 0, 0], 10, 10, 10) pts = find.colors(img, cts) clusters = pts.cluster(2) if imgshow: img = draw_text(img, "test_pa_cluster", (0, 0)) clusters.draw(img).show() self.assertGreaterEqual(len(clusters), 1)
def test_pa2d_filter(self): # test core/types/point_array2d.py img = Image.open('test/test.jpeg') cts = CTS2([0, 0, 0], 10, 10, 10) pts = find.colors(img, cts) clusters = pts.cluster(2) filtered = clusters.filtersize(50, 3000) if imgshow: img = draw_text(img, "test_pa2d_filter", (0, 0)) filtered.draw(img).show() self.assertEqual(len(filtered), 1)
def draw_text(img: Image, text: str, pos: Point, fontname='UpChars07', color=[255, 0, 0]): if type(pos) is tuple: pos = Point(pos[0], pos[1]) img = img.copy() textimg = text_to_img(text) y, x, _ = np.nonzero(textimg) # indices of nonblack values points = zip(y, x) for p in points: img[p[0] + pos.y, p[1] + pos.x] = color return img
def image_exact(haystack: Image, needle: Image) -> List[Box]: """ Find an image (exactly) within another image. Parameters ---------- haystack the image to be looked in needle the image to be found Returns ------- matches bounding boxes around any matches """ matches = [] needle = needle.copy() needle[np.where(needle.mask is True)] = [0, 0, 0] sheight, swidth, _ = needle.shape ty, tx, _ = haystack.shape # total x,y if swidth > tx or sheight > ty: raise Exception('Needle is larger than haystack.') for y in range(ty): # iterate y 1st cus text travels horizontal if y + sheight > ty: break for x in range(tx): if x + swidth > tx: break s_haystack = haystack.copy()[y:y + sheight, x:x + swidth] s_haystack[np.where(needle.mask is True)] = [0, 0, 0] needle.mask = False # remove the mask cus we dont need it no more if np.all(needle[0, 0] == s_haystack[0, 0]): if np.all(needle == s_haystack): match = Box.from_array([x, y, x + swidth, y + sheight]) matches.append(match) return matches
def draw_image(src: Image, insert: Image, pos: Point) -> Image: src = src.copy() My, Mx, _ = src.shape # max y, x i_h, i_w, _ = insert.shape # insert height, width if pos.x + i_w > Mx or pos.y + i_h > My: raise Exception('Can\'t insert image {} into image {} at {},{}') \ .format(insert.shape, src.shape, pos.x, pos.y) else: ys = range(0, i_h) xs = range(0, i_w) for y in ys: for x in xs: if not np.all(insert.mask[y, x]): src[pos.y + y, pos.x + x] = insert[y, x] return src
def get_image(self) -> Image: # get the shape of the window and reload canvas if it fails # canvas id changes on resize, other events cause this too ? try: g = self.canvas.get_geometry() except Xerror.BadDrawable or Xerror.BadMatch: self.update() g = self.canvas.get_geometry() raw = self.canvas.get_image(0, 0, g.width, g.height, X.ZPixmap, 0xffffffff) if type(raw.data) is str: # case: window covered # TODO: handle minimized window? ewmh.setActiveWindow(self.frame) ewmh.display.flush() # force targeted window to top -- doesnt work raw = self.canvas.get_image(0, 0, g.width, g.height, X.ZPixmap, 0xffffffff) img = Image.frombytes((g.width, g.height), raw.data) return img
def get_image(self, img: Image) -> Image: img = self.get_image_slice(img) img.mask = self.mask img.alpha_mask = True return img
def __init__(self, p1: Point, mask: str = './test/minimap-border.png'): masked_img = Image.open(mask, alpha_mask=True) self.mask = masked_img.mask # we want the mask only h, w, _ = self.mask.shape p2 = Point(p1.x + w, p1.y + h) super().__init__(p1, p2)
# row 5 inv_slots.append(Slot.from_array([563, 359, 594, 390])) inv_slots.append(Slot.from_array([604, 359, 635, 391])) inv_slots.append(Slot.from_array([644, 359, 680, 391])) inv_slots.append(Slot.from_array([688, 361, 720, 389])) # row 6 inv_slots.append(Slot.from_array([562, 398, 595, 426])) inv_slots.append(Slot.from_array([606, 397, 635, 424])) inv_slots.append(Slot.from_array([644, 394, 680, 426])) inv_slots.append(Slot.from_array([687, 396, 723, 426])) # row 7 inv_slots.append(Slot.from_array([561, 429, 597, 462])) inv_slots.append(Slot.from_array([605, 431, 638, 459])) inv_slots.append(Slot.from_array([646, 433, 680, 460])) inv_slots.append(Slot.from_array([690, 431, 722, 460])) # INVENTORY interface_tabs_boxr = [552, 205, 733, 464] # interface tab box raw inv_button = Button.from_array([628, 170, 658, 201]) inventory = Inventory(interface_tabs_boxr, inv_button, inv_slots) # MINIMAP/COMPASS mmap_pos = Point(643, 83) # center of mmap comp_pos = Point(561, 20) # center of compass minimap = Minimap(Point(550, 4)) compass = Compass(comp_pos, 15) testimg = Image.open( '/home/not-here/Projects/pysrl/scripts/scaperune/images/blah.png') chatbox = Chatbox.from_array([4, 368, 517, 503]) mainscreen = Box.from_array([4, 4, 516, 338])