Beispiel #1
0
    def apply_left(self, img: PILImage,
                   text_bbox: BBox) -> Tuple[PILImage, BBox]:
        in_offset, thickness, out_offset = self._get_lr_param()

        new_w = img.width + thickness + in_offset + out_offset
        new_h = img.height

        new_img = transparent_img((new_w, new_h))
        new_img.paste(img, (thickness + in_offset + out_offset, 0))

        draw = ImageDraw.Draw(new_img)

        text_bbox.offset_(text_bbox.right_top, (new_w, 0))
        text_bbox.left -= in_offset

        draw.line(
            list(text_bbox.left_top) + list(text_bbox.left_bottom),
            fill=self._get_line_color(img, text_bbox),
            width=thickness,
        )

        text_bbox.left -= thickness
        text_bbox.left -= out_offset

        return new_img, text_bbox
Beispiel #2
0
    def apply(self, img: PILImage, text_bbox: BBox) -> Tuple[PILImage, BBox]:
        w_ratio = np.random.uniform(*self.w_ratio)
        h_ratio = np.random.uniform(*self.h_ratio)
        new_w = int(img.width + img.width * w_ratio)
        new_h = int(img.height + img.height * h_ratio)

        new_img = transparent_img((new_w, new_h))
        xy = random_xy_offset(img.size, (new_w, new_h))
        new_img.paste(img, xy)

        new_bbox = text_bbox.move_origin(xy)
        return new_img, new_bbox
    def gen_multi_corpus(self) -> Tuple[PILImage, str]:
        font_texts: List[FontText] = [it.sample() for it in self.corpus]

        bg = self.bg_manager.get_bg()

        text_color = None
        if self.cfg.text_color_cfg is not None:
            text_color = self.cfg.text_color_cfg.get_color(bg)

        text_masks, text_bboxes = [], []
        for i in range(len(font_texts)):
            font_text = font_texts[i]

            if text_color is None:
                _text_color = self.corpus[i].cfg.text_color_cfg.get_color(bg)
            else:
                _text_color = text_color
            text_mask = draw_text_on_bg(
                font_text,
                _text_color,
                char_spacing=self.corpus[i].cfg.char_spacing)

            text_bbox = BBox.from_size(text_mask.size)
            if self.cfg.corpus_effects is not None:
                effects = self.cfg.corpus_effects[i]
                if effects is not None:
                    text_mask, text_bbox = effects.apply_effects(
                        text_mask, text_bbox)
            text_masks.append(text_mask)
            text_bboxes.append(text_bbox)

        text_mask_bboxes, merged_text = self.layout(
            font_texts,
            [it.copy() for it in text_bboxes],
            [BBox.from_size(it.size) for it in text_masks],
        )
        if len(text_mask_bboxes) != len(text_bboxes):
            raise PanicError(
                "points and text_bboxes should have same length after layout output"
            )

        merged_bbox = BBox.from_bboxes(text_mask_bboxes)
        merged_text_mask = transparent_img(merged_bbox.size)
        for text_mask, bbox in zip(text_masks, text_mask_bboxes):
            merged_text_mask.paste(text_mask, bbox.left_top)

        if self.cfg.perspective_transform is not None:
            transformer = PerspectiveTransform(self.cfg.perspective_transform)
            # TODO: refactor this, now we must call get_transformed_size to call gen_warp_matrix
            _ = transformer.get_transformed_size(merged_text_mask.size)

            (
                transformed_text_mask,
                transformed_text_pnts,
            ) = transformer.do_warp_perspective(merged_text_mask)
        else:
            transformed_text_mask = merged_text_mask

        if self.cfg.layout_effects is not None:
            transformed_text_mask, _ = self.cfg.layout_effects.apply_effects(
                transformed_text_mask,
                BBox.from_size(transformed_text_mask.size))

        img = self.paste_text_mask_on_bg(bg, transformed_text_mask)

        return img, merged_text