def read_text(self, locator: Optional[str] = None, invert: bool = False): """Read text using OCR from the screen, or an area of the screen defined by the given locator. :param locator: Location of element to read text from :param invert: Invert image colors, useful for reading white text on dark background """ ensure_recognition() if locator is not None: element = self.ctx.wait_for_element(locator) if not isinstance(element, Region): raise ValueError("Locator must resolve to a region") self.logger.info("Reading text from element: %s", element) image = screen.grab(element) else: self.logger.info("Reading text from screen") image = screen.grab() screen.log_image(image) if invert: image = ImageOps.invert(image) start_time = time.time() text = ocr.read(image) self.logger.info("Read text in %.2f seconds", time.time() - start_time) return text
def read_text(self, locator: Optional[str] = None, invert: bool = False): """Read text using OCR from the screen, or an area of the screen defined by the given locator. :param locator: Location of element to read text from :param invert: Invert image colors, useful for reading white text on dark background Usage examples: .. code-block:: robotframework ${label_region}= Find Element image:label.png ${value_region}= Move Region ${label_region} 100 0 ${text}= Read Text ${value_region} .. code-block:: python label_region = desktop.find_element("image:label.png") value_region = desktop.move_region(label_region, 100, 0) text = desktop.read_text(value_region) """ ensure_recognition() if locator is not None: element = self.ctx.wait_for_element(locator) if not isinstance(element, Region): raise ValueError("Locator must resolve to a region") self.logger.info("Reading text from element: %s", element) image = screen.grab(element) else: self.logger.info("Reading text from screen") image = screen.grab() screen.log_image(image) if invert: image = ImageOps.invert(image) start_time = time.time() text = ocr.read(image) self.logger.info("Read text in %.2f seconds", time.time() - start_time) return text
def _find_from_displays( self, finder: Callable[[Image.Image], List[Region]]) -> List[Region]: """Call finder function for each display and return a list of found regions. :param finder: Callable that searches an image """ matches = [] screenshots = [] # Search all displays, and map results to combined virtual display start_time = time.time() for display in screen.displays(): image = screen.grab(display) regions = finder(image) for region in regions: region = region.resize(5) screenshot = image.crop(region.as_tuple()) screenshots.append(screenshot) local = Region.from_size(0, 0, image.size[0], image.size[1]) regions = transform(regions, local, display) matches.extend(regions) # Log matches and preview images duration = time.time() - start_time plural = "es" if len(matches) != 1 else "" self.logger.info("Searched in %.2f seconds", duration) self.logger.info("Found %d match%s", len(matches), plural) for match, screenshot in zip(matches, screenshots): screen.log_image(screenshot, size=400) self.logger.info(match) return matches