def paste_text_mask_on_bg( self, bg: PILImage, transformed_text_mask: PILImage) -> Tuple[PILImage, PILImage]: """ Args: bg: transformed_text_mask: Returns: """ x_offset, y_offset = utils.random_xy_offset(transformed_text_mask.size, bg.size) bg = self.bg_manager.guard_bg_size(bg, transformed_text_mask.size) bg = bg.crop(( x_offset, y_offset, x_offset + transformed_text_mask.width, y_offset + transformed_text_mask.height, )) if self.cfg.return_bg_and_mask: _bg = bg.copy() else: _bg = bg bg.paste(transformed_text_mask, (0, 0), mask=transformed_text_mask) return bg, _bg
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