Beispiel #1
0
    def get_float_number_from_zone(self,
                                   cache=False,
                                   zone=None,
                                   lang=None,
                                   resize_percent=0,
                                   resize=0,
                                   contrast=0,
                                   invert=False,
                                   brightness=0,
                                   change_mode=True,
                                   tessdata_dir=None):
        """Returns the *float*. For details check `Get Number From Zone`
            Examples:
            | Get Float Number From Zone | zone=[x  y  w  h] | lang=eng | resize_percent=15 | resize=0.95 | contrast=0.1 | background=${None} | invert=${False} |
            brightness=0 | change_mode=${True}
        """

        self.window_area = GUIProcess().get_window_area()

        img = ImageProcessor(self.error_handler,
                             self.screenshot_folder).get_image_to_recognize(
                                 zone, cache, resize_percent, contrast, invert,
                                 brightness, change_mode, self.window_area)

        if int(resize) > 0:
            resize = int(resize)
            origFile = ImageProcessor(
                self.error_handler,
                self.screenshot_folder)._make_up_filename()
            img.save(origFile)
            origresize = Image.open(origFile)
            width, height = origresize.size
            width_resize = width * int(resize) / width + width
            height_resize = height * int(resize) / height + height
            img = origresize.resize(
                (int(round(width_resize)), int(round(height_resize))),
                Image.ANTIALIAS)

        img.save(
            os.path.join(self.screenshot_folder,
                         'ImageLibrary_Zone_Image.png'))

        config = ""
        if lang:
            mydir = os.path.abspath(os.path.dirname(__file__))
            resdir = os.path.abspath(os.path.join(os.sep, mydir, tessdata_dir))
            config += ("--tessdata-dir %s -l %s " % (resdir, lang)).replace(
                "\\", "//")
        config += "-psm 8 -c tessedit_char_whitelist=.,0123456789"

        txt = image_to_string(img, config=config)
        txt = float(txt)
        try:
            return txt
        except ValueError as e:
            msg = "Error while parsing number: " + str(e)
            ErrorHandler(self.screenshot_folder).report_error(
                msg, ("img", img))
            raise
    def get_image_from_zone(self, zone, image_name):
        """Returns image from given zone and saves it to 'output' under the provided image_name or default"""
        screen = self.get_area(zone)
        scr = ImageProcessor()._get_screenshot(screen)
        if image_name is not None:
            screen_name = image_name
        else:
            screen_name = 'image_from_zone'

        try:
            output = BuiltIn().get_variable_value('${OUTPUT_DIR}')
        except RobotNotRunningError:
            LOGGER.info('Could not get output dir, using default - output')
            output = os.path.join(os.getcwd(), 'output')

        scr.save(output + '\\' + screen_name + '.png')

        return output + '\\' + screen_name + '.png'
Beispiel #3
0
    def get_image_from_zone(self, zone=None):
        """Returns the image from zone. Saves image to the screenshot folder as image_from_zone.png.
            Examples:
            | Get Number With Text From Zone | zone=[x  y  w  h]

        """

        if zone is None:
            raise Exception('Search zone should be passed as arg.')
        else:
            scr = ImageProcessor(self.error_handler,
                                 self.screenshot_folder)._screenshot(zone)

            try:
                scr.save(self.screenshot_folder + '\\image_from_zone.png')
                a = self.screenshot_folder + '\\image_from_zone.png'
                return a
            except:
                LOGGER.warn('Could not get screenshot folder dir')
    def get_image_from_zone(self, zone=None):
        """Returns the image from zone. Saves image to the screenshot folder as image_from_zone.png.
            Examples:
            | Get Number With Text From Zone | zone=[x  y  w  h]

        """

        if zone is None:
            raise Exception('Search zone should be passed as argument.')
        else:
            scr = ImageProcessor(self.error_handler,
                                 self.screenshot_folder)._screenshot(zone)
            try:
                if not os.path.exists(self.screenshot_folder):
                    os.makedirs(self.screenshot_folder)
                result = os.path.join(self.screenshot_folder,
                                      'image_from_zone.png')
                scr.save(result)
                return result
            except:
                LOGGER.warn('Could not get screenshot folder dir')
Beispiel #5
0
    def get_number_from_zone(self,
                             cache=False,
                             zone=None,
                             lang=None,
                             resize_percent=0,
                             resize=0,
                             contrast=0,
                             background=None,
                             invert=False,
                             brightness=0,
                             change_mode=True,
                             tessdata_dir=None):
        """Returns an integer from the given zone. NB! If zone is not provided the whole acctive window is taken.

            Examples:
            | Get Number From Zone | zone=[x  y  w  h] | lang=eng | resize_percent=15 | resize=0.95 | contrast=0.1 | background=${None} | invert=${False} |
            brightness=0 | change_mode=${True}

            *resize_percent* - changes the screeshot size right after it is taken\n

            vs *resize* - changes the screenshot size after all other operations with screenshot (contrast, invert, etc)\n

            *contrast* - changes the contrast of the screeshot\n
            *background* - provide the pattern image of the same size, as the keyword zone. As param value pass the pattern location\n
            the common parts of two images will be removed returning the difference image.\n
            *invert* - inverts the colors (mind that it inverts the black-white image; if you want to keep the original colors, use with \n
            change_mode=${False} param).
            *change_mode* - by default is ${True}, changes to black-white format. If you need to keep the original - pass ${False)


        """

        self.window_area = GUIProcess().get_window_area()

        if background is not None:
            img = ImageProcessor(
                self.error_handler,
                self.screenshot_folder).get_image_to_recognize_with_background(
                    zone, cache, resize_percent, contrast, background,
                    brightness, invert)
        else:
            img = ImageProcessor(
                self.error_handler,
                self.screenshot_folder).get_image_to_recognize(
                    zone, cache, resize_percent, contrast, invert, brightness,
                    change_mode, self.window_area)

        if int(resize) > 0:
            resize = int(resize)
            origFile = ImageProcessor(
                self.error_handler,
                self.screenshot_folder)._make_up_filename()
            img.save(origFile)
            origresize = Image.open(origFile)
            width, height = origresize.size
            width_resize = width * int(resize) / width + width
            height_resize = height * int(resize) / height + height
            img = origresize.resize(
                (int(round(width_resize)), int(round(height_resize))),
                Image.ANTIALIAS)

        img.save(
            os.path.join(self.screenshot_folder,
                         'ImageLibrary_Zone_Image.png'))

        config = ""
        if lang:
            mydir = os.path.abspath(os.path.dirname(__file__))
            resdir = os.path.abspath(os.path.join(os.sep, mydir, tessdata_dir))
            config += ("--tessdata-dir %s -l %s " % (resdir, lang)).replace(
                "\\", "//")
        config += "-psm 8 -c tessedit_char_whitelist=0123456789"

        txt = image_to_string(img, config=config)

        try:
            return int(txt)
        except ValueError as e:
            msg = "Error while parsing number: " + str(e)
            ErrorHandler(self.screenshot_folder).report_error(
                msg, ("img", img))
            raise
Beispiel #6
0
    def get_number_with_text_from_zone(self,
                                       cache=False,
                                       zone=None,
                                       lang=None,
                                       resize_percent=0,
                                       resize=0,
                                       contrast=0,
                                       background=None,
                                       invert=False,
                                       brightness=0,
                                       change_mode=True,
                                       tessdata_dir=None):
        """Returns only number from zone with text and number. For arguments details check `Get Number From Zone`
            Examples:
            | Get Number With Text From Zone | zone=[x  y  w  h] | lang=eng | resize_percent=15 | resize=0.95 | contrast=0.1 | background=${None} | invert=${False} |
            brightness=0 | change_mode=${True}
        """

        self.window_area = GUIProcess().get_window_area()

        if background is not None:
            img = ImageProcessor(
                self.error_handler,
                self.screenshot_folder).get_image_to_recognize_with_background(
                    zone, cache, resize_percent, contrast, invert, brightness,
                    change_mode)
        else:
            img = ImageProcessor(
                self.error_handler,
                self.screenshot_folder).get_image_to_recognize(
                    zone, cache, resize_percent, contrast, invert, brightness,
                    change_mode, self.window_area)

        if int(resize) > 0:
            resize = int(resize)
            origFile = ImageProcessor(
                self.error_handler,
                self.screenshot_folder)._make_up_filename()
            img.save(origFile)
            origresize = Image.open(origFile)
            width, height = origresize.size
            width_resize = width * int(resize) / width + width
            height_resize = height * int(resize) / height + height
            img = origresize.resize(
                (int(round(width_resize)), int(round(height_resize))),
                Image.ANTIALIAS)

        img.save(
            os.path.join(self.screenshot_folder,
                         'ImageLibrary_Zone_Image.png'))

        config = ""
        if lang:
            mydir = os.path.abspath(os.path.dirname(__file__))
            resdir = os.path.abspath(os.path.join(os.sep, mydir, tessdata_dir))
            config += ("--tessdata-dir %s -l %s " % (resdir, lang)).replace(
                "\\", "//")
        config += "-psm 6"

        txt = image_to_string(img, config=config)

        num = re.compile('[0-9|.0-9]+')
        num = num.findall(txt)
        num = ''.join(num)

        try:
            return float(num)
        except ValueError as e:
            msg = "Error while parsing text:" + str(e)
            ErrorHandler(self.screenshot_folder).report_error(
                msg, ("img", img))
            raise