Example #1
0
    def auto_gen_next_img(self, width, height, strategy, bg_img, block_list):
        """
        自动生成下一个文本贴图
        :return:
        """
        from service import text_provider
        text = "".join(text_provider.gen.__next__())
        fp = self.next_font_path()

        if isinstance(strategy, HorizontalStrategy):
            orientation = TYPE_ORIENTATION_VERTICAL
        elif isinstance(strategy, VerticalStrategy):
            orientation = TYPE_ORIENTATION_HORIZONTAL
        elif isinstance(strategy, HorizontalFlowStrategy):
            orientation = TYPE_ORIENTATION_HORIZONTAL
        elif isinstance(strategy, VerticalFlowStrategy):
            orientation = TYPE_ORIENTATION_VERTICAL
        elif isinstance(strategy, CustomizationStrategy1):
            if block_list:
                orientation = TYPE_ORIENTATION_HORIZONTAL
            else:
                orientation = TYPE_ORIENTATION_VERTICAL
        else:
            orientation = Random.random_choice_list([
                TYPE_ORIENTATION_VERTICAL, TYPE_ORIENTATION_HORIZONTAL,
                TYPE_ORIENTATION_HORIZONTAL
            ])

        v = min(width, height)
        # 设置字体大小

        font_size = Random.random_int(v // 20, v // 10)
        font_size = self.font_min_size if font_size < self.font_min_size else font_size

        # 剔除不存在的文字
        text = "".join(filter(lambda c: font_tool.check(c, font_path=fp),
                              text))
        if len(text) >= 2:
            # 生成文本图片
            align = Random.random_choice_list(
                [TYPE_ALIGN_MODEL_B, TYPE_ALIGN_MODEL_T, TYPE_ALIGN_MODEL_C])
            text_img = self.gen_text_img(text,
                                         font_size=font_size,
                                         border_width=self.char_border_width,
                                         border_color=self.char_border_color,
                                         color=self.get_fontcolor(bg_img),
                                         orientation=orientation,
                                         align_mode=align,
                                         font_path=fp)
            return text_img
Example #2
0
    def get_fontcolor(self, bg_img):
        """
        get font color by mean
        :param bg_img:
        :return:
        """
        char_common_color_list = self.char_common_color_list

        if Random.random_float(
                0, 1
        ) <= self.use_char_common_color_probability and char_common_color_list:
            return eval(Random.random_choice_list(char_common_color_list))
        else:
            image = np.asarray(bg_img)
            lab_image = cv2.cvtColor(image, cv2.COLOR_RGB2Lab)

            bg = lab_image[:, :, 0]
            l_mean = np.mean(bg)

            new_l = Random.random_int(
                0, 127 -
                80) if l_mean > 127 else Random.random_int(127 + 80, 255)
            new_a = Random.random_int(0, 255)
            new_b = Random.random_int(0, 255)

            lab_rgb = np.asarray([[[new_l, new_a, new_b]]], np.uint8)
            rbg = cv2.cvtColor(lab_rgb, cv2.COLOR_Lab2RGB)

            r = rbg[0, 0, 0]
            g = rbg[0, 0, 1]
            b = rbg[0, 0, 2]

            return (r, g, b, 255)
Example #3
0
    def apply_gauss_blur(self, ks=None):
        """

        :param ks: guass kernal window size
        :return:
        """
        bg_high = Random.random_float(220, 255)
        bg_low = bg_high - Random.random_float(1, 60)
        width = Random.random_int(self._width_range[0], self._width_range[1])
        height = Random.random_int(self._height_range[0], self._height_range[1])
        img = np.random.randint(bg_low, bg_high, (height, width)).astype(np.uint8)
        if ks is None:
            ks = [7, 9, 11, 13]
        k_size = Random.random_choice_list(ks)
        sigmas = [0, 1, 2, 3, 4, 5, 6, 7]
        sigma = 0
        if k_size <= 3:
            sigma = Random.random_choice_list(sigmas)
        img = cv2.GaussianBlur(img, (k_size, k_size), sigma)
        return img