def draw_text_wrapper(self, draw, text, x, y, font, text_color, font2, pil_img): """ :param x/y: 应该是移除了 offset 的 """ if apply(self.cfg.text_border): # print("error") self.draw_border_text(draw, text, x, y, font, text_color) # todo: 整合draw_text_wrapper 与 draw_text_with_random_space elif apply(self.cfg.second_font): if self.cfg.second_font.font_color_change: text_color2 = self.get_word_color() else: text_color2 = text_color for i, c in enumerate(text): if random.random() < self.cfg.second_font.change_rate and not self.fonts_by_image: y_offset = np.random.uniform(0, font2.getoffset(c)[1]) draw.text((x, y + y_offset), c, fill=text_color2, font=font2) x += font2.getsize(c)[0] else: if self.fonts_by_image: pil_img = self.mix_seamless_bg(font[c], pil_img, (x, y)) x += font[c].shape[1] else: draw.text((x, y), c, fill=text_color, font=font) x += font.getsize(c)[0] else: if self.fonts_by_image: # print("pil_img", pil_img.shape) for c in text: pil_img = self.mix_seamless_bg(font[c], pil_img, (x, y)) x += font[c].shape[1] else: draw.text((x, y), text, fill=text_color, font=font) return pil_img
def gen_img(self, word_img): #if apply(self.cfg.noise): # word_img = np.clip(word_img, 0., 255.) # word_img = self.noiser.apply(word_img) blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg('After blur') if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg('After prydown1') else: word_img = self.apply_prydown(word_img) self.dmsg('After prydown2') word_img = np.clip(word_img, 0., 255.) #if apply(self.cfg.reverse_color): # word_img = self.reverse_img(word_img) # self.dmsg('After reverse_color') #if apply(self.cfg.emboss): # word_img = self.apply_emboss(word_img) # self.dmsg('After emboss') #if apply(self.cfg.sharp): # word_img = self.apply_sharp(word_img) # self.dmsg('After sharp') word_img = np.clip(word_img, 0., 255.) word_img = word_img.astype(np.uint8) return word_img
def draw_text_on_bg(self, word, font, bg): """ Draw word in the center of background :param word: word to draw :param font: font to draw word :param bg: background numpy image :return: np_img: word image text_box_pnts: left-top, right-top, right-bottom, left-bottom """ bg_height = bg.shape[0] bg_width = bg.shape[1] word_size = self.get_word_size(font, word) word_height = word_size[1] word_width = word_size[0] offset = font.getoffset(word) pil_img = Image.fromarray(np.uint8(bg)) draw = ImageDraw.Draw(pil_img) # Draw text in the center of bg text_x = int((bg_width - word_width) / 2) text_y = int((bg_height - word_height) / 2) if self.is_bgr(): word_color = self.get_word_color() else: word_color = self.get_gray_word_color(bg, text_x, text_y, word_height, word_width) if apply(self.cfg.random_space): text_x, text_y, word_width, word_height = self.draw_text_with_random_space( draw, font, word, word_color, bg_width, bg_height) np_img = np.array(pil_img).astype(np.float32) else: if apply(self.cfg.seamless_clone): np_img = self.draw_text_seamless(font, bg, word, word_color, word_height, word_width, offset) else: self.draw_text_wrapper(draw, word, text_x - offset[0], text_y - offset[1], font, word_color) # draw.text((text_x - offset[0], text_y - offset[1]), word, fill=word_color, font=font) np_img = np.array(pil_img).astype(np.float32) text_box_pnts = [[text_x, text_y], [text_x + word_width, text_y], [text_x + word_width, text_y + word_height], [text_x, text_y + word_height]] return np_img, text_box_pnts, word_color
def gen_img(self): word, font, word_size = self.pick_font() # Background's height should much larger than raw word image's height, # to make sure we can crop full word image after apply perspective bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) word_img, text_box_pnts, word_color = self.draw_text_on_bg( word, font, bg) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=self.cfg.perspective_transform.max_x, max_y=self.cfg.perspective_transform.max_y, max_z=self.cfg.perspective_transform.max_z, gpu=self.gpu) if self.debug: word_img = draw_box(word_img, img_pnts_transformed, (0, 255, 0)) word_img = draw_box(word_img, text_box_pnts_transformed, (0, 0, 255)) _, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) word_img = draw_bbox(word_img, crop_bbox, (255, 0, 0)) else: word_img, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) return word_img, word
def draw_text_wrapper(self, draw, text, x, y, font, text_color): """ :param x/y: 应该是移除了 offset 的 """ if apply(self.cfg.text_border): self.draw_border_text(draw, text, x, y, font, text_color) else: draw.text((x, y), text, fill=text_color, font=font)
def gen_bg(self, width, height): if apply(self.cfg.img_bg): bg = self.gen_bg_from_image(int(width), int(height)) else: bg = self.gen_rand_bg(int(width), int(height)) while bg is None: bg = self.gen_rand_bg(int(width), int(height)) return bg
def gen_img(self, img_index): word, font, word_size = self.pick_font(img_index) if self.space_ratio > 0 and len(word) > 2: word = list(word) for i in range(1, len(word) - 1): if word[i-1] != ' ' and random.random() < self.space_ratio: # 不允许出现连续的空格 word[i] = ' ' word = ''.join(word) self.dmsg("after pick font") # Background's height should much larger than raw word image's height, # to make sure we can crop full word image after apply perspective bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) word_img, text_box_pnts, word_color = self.draw_text_on_bg(word, font, bg) self.dmsg("After draw_text_on_bg") if apply(self.cfg.crop): text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("After draw line") if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply(word_img, text_box_pnts, word_color) self.dmsg("After remapping") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=self.cfg.perspective_transform.max_x, max_y=self.cfg.perspective_transform.max_y, max_z=self.cfg.perspective_transform.max_z, gpu=self.gpu) self.dmsg("After perspective transform") if self.debug: _, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) word_img = draw_bbox(word_img, crop_bbox, (255, 0, 0)) else: word_img, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) self.dmsg("After crop_img") if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("After prydown") word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") # word_img = cv2.resize(word_img, None, fx=0.5, fy=0.5) return word_img, word
def gen_img(self, img_index): word, font, word_size = self.pick_font(img_index) self.dmsg("after pick font") # Background's height should much larger than raw word image's height, # to make sure we can crop full word image after apply perspective bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) word_img, text_box_pnts, word_color = self.draw_text_on_bg( word, font, bg) self.dmsg("After draw_text_on_bg") if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("After draw line") if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply( word_img, text_box_pnts, word_color) self.dmsg("After remapping") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=self.cfg.perspective_transform.max_x, max_y=self.cfg.perspective_transform.max_y, max_z=self.cfg.perspective_transform.max_z, gpu=self.gpu) self.dmsg("After perspective transform") if self.debug: _, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) word_img = draw_bbox(word_img, crop_bbox, (255, 0, 0)) else: word_img, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) self.dmsg("After crop_img") if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("After prydown") word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") return word_img, word
def gen_img(self, img_index): word, font, word_size = self.pick_font(img_index) self.dmsg("选择字体后") # 背景的高度应该远远大于原始文字图像的高度, # 以确保我们可以裁剪完整的字图像后,应用透视 bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) # 在背景图上写字,text_box_pnts,文字位置 word_img, text_box_pnts, word_color = self.draw_text_on_bg( word, font, bg) self.dmsg("将文字渲染到背景图完成") if apply(self.cfg.crop): text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("将线条渲染完成") if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply( word_img, text_box_pnts, word_color) self.dmsg("重新映射完成") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=self.cfg.perspective_transform.max_x, max_y=self.cfg.perspective_transform.max_y, max_z=self.cfg.perspective_transform.max_z, gpu=self.gpu) self.dmsg("角度变换完成") if self.debug: _, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) word_img = draw_bbox(word_img, crop_bbox, (255, 0, 0)) else: word_img, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) self.dmsg("裁切图片完成") # self.cfg配置参数 if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("噪点处理完毕") blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("模糊处理完毕") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("prydown处理完毕") word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("反转颜色处理完毕") if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("浮雕处理完毕") if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("锐化处理完毕") return word_img, word
def draw_text_on_bg(self, word, font, bg, dir): """ Draw word in the center of background :param word: word to draw :param font: font to draw word :param bg: background numpy image :return: np_img: word image text_box_pnts: left-top, right-top, right-bottom, left-bottom """ if dir == "vertical": v_bg = np.rot90(bg, -1) bg_height = v_bg.shape[0] bg_width = v_bg.shape[1] word_size = self.get_word_size(font, word) word_height = word_size[1] word_width = word_size[0] offset = font.getoffset(word) pil_img = Image.fromarray(np.uint8(v_bg)) # img = cv2.cvtColor(np.asarray(pil_img), cv2.COLOR_RGB2BGR) # cv2.imshow("1",img) # cv2.waitKey(0) draw = ImageDraw.Draw(pil_img) # Draw text in the center of bg text_x = int((bg_width - word_width / len(word)) / 2) text_y = int((bg_height - word_height) / 2) leftx, lefty, rightx, righty = 0, 0, 0, 0 if self.is_bgr(): word_color = self.get_word_color() else: word_color = self.get_gray_word_color(v_bg, text_x, text_y, word_height, word_width) if apply(self.cfg.random_space): text_x, text_y, word_width, word_height = self.draw_text_with_random_space( draw, font, word, word_color, bg_width, bg_height, dir) h_bg = np.rot90(pil_img, 1) leftx, lefty = text_y, text_x rightx, righty = leftx + word_height, lefty + word_width np_img = np.array(h_bg).astype(np.float32) else: # self.draw_text_wrapper(draw, word, text_x - offset[0], text_y - offset[1], font, word_color) # draw.text((text_x - offset[0], text_y - offset[1]), word, fill=word_color, font=font) startx, starty = text_x - offset[0], text_y - offset[1] leftx, lefty = int(starty), int(startx) border_color = None if apply(self.cfg.text_border): for item in word: # draw.text((startx, starty), item, fill=word_color, font=font) border_color = self.draw_border_text( draw, item, startx, starty, font, word_color, border_color) # img = cv2.cvtColor(np.asarray(pil_img), cv2.COLOR_RGB2BGR) # cv2.imshow("1",img) # cv2.waitKey(0) starty += word_height else: for item in word: draw.text((startx, starty), item, fill=word_color, font=font) starty += word_height rightx = int(starty) + word_height first_word_width = font.getsize(word[0])[0] righty = int(lefty + first_word_width) h_bg = np.rot90(pil_img, 1) np_img = np.array(h_bg).astype(np.float32) text_box_pnts = [[leftx, lefty], [rightx, lefty], [rightx, righty], [leftx, righty]] # img = cv2.cvtColor(np.asarray(pil_img), cv2.COLOR_RGB2BGR) # # res = cv2.rectangle(img, (leftx, lefty), (rightx, righty), (0, 0, 255), 20) # cv2.imshow("1", img) # cv2.waitKey(0) return np_img, text_box_pnts, word_color else: bg_height = bg.shape[0] bg_width = bg.shape[1] word_size = self.get_word_size(font, word) word_height = word_size[1] word_width = word_size[0] offset = font.getoffset(word) pil_img = Image.fromarray(np.uint8(bg)) draw = ImageDraw.Draw(pil_img) # Draw text in the center of bg text_x = int((bg_width - word_width) / 2) text_y = int((bg_height - word_height) / 2) if self.is_bgr(): word_color = self.get_word_color() else: word_color = self.get_gray_word_color(bg, text_x, text_y, word_height, word_width) if apply(self.cfg.random_space): text_x, text_y, word_width, word_height = self.draw_text_with_random_space( draw, font, word, word_color, bg_width, bg_height, dir) np_img = np.array(pil_img).astype(np.float32) else: if apply(self.cfg.seamless_clone): np_img = self.draw_text_seamless(font, bg, word, word_color, word_height, word_width, offset) else: self.draw_text_wrapper(draw, word, text_x - offset[0], text_y - offset[1], font, word_color) # draw.text((text_x - offset[0], text_y - offset[1]), word, fill=word_color, font=font) np_img = np.array(pil_img).astype(np.float32) text_box_pnts = [[text_x, text_y], [text_x + word_width, text_y], [text_x + word_width, text_y + word_height], [text_x, text_y + word_height]] return np_img, text_box_pnts, word_color
def gen_img(self, img_index, inv_alph_dict): # not blur image, like real image is_pass = random.random() < 0.2 while True: # check word is valid # limit font size to avoid to scale word img word, font, word_size = self.pick_font(img_index, 25 if is_pass else None) # delete invalid word invalid = False for c in word: if c not in inv_alph_dict: invalid = True break if not invalid: break if self.space_ratio > 0 and len(word) > 2: word = list(word) for i in range(1, len(word) - 1): if word[i-1] != ' ' and random.random() < self.space_ratio: # 不允许出现连续的空格 word[i] = ' ' word = ''.join(word) self.dmsg("after pick font") # Background's height should much larger than raw word image's height, # to make sure we can crop full word image after apply perspective bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) word_img, text_box_pnts, word_color = self.draw_text_on_bg(word, font, bg, is_pass) self.dmsg("After draw_text_on_bg") if not is_pass and apply(self.cfg.crop): text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("After draw line") if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply(word_img, text_box_pnts, word_color) self.dmsg("After remapping") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) if is_pass: # avoid to rotation img # the font size is 25 word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=0.01, max_y=0.01, max_z=0.01, gpu=self.gpu) else: word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=self.cfg.perspective_transform.max_x, max_y=self.cfg.perspective_transform.max_y, max_z=self.cfg.perspective_transform.max_z, gpu=self.gpu) self.dmsg("After perspective transform") if self.debug: _, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) word_img = draw_bbox(word_img, crop_bbox, (255, 0, 0)) else: word_img, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) self.dmsg("After crop_img") if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") blured = False if not is_pass: if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("After prydown") word_img = np.clip(word_img, 0., 255.) if not is_pass and apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") if not is_pass and apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") if not is_pass and apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") # word_img = cv2.resize(word_img, None, fx=0.5, fy=0.5) word_img = word_img.astype(np.uint8) return word_img, word
def draw_text_on_bg(self, word, font, bg, font2): """ Draw word in the center of background :param word: word to draw :param font: font to draw word :param bg: background numpy image :return: np_img: word image text_box_pnts: left-top, right-top, right-bottom, left-bottom """ bg_height = bg.shape[0] bg_width = bg.shape[1] word_size = self.get_word_size(font, word) word_height = word_size[1] word_width = word_size[0] offset = (0, 0) if self.fonts_by_image else font.getoffset(word) pure_bg = np.ones((bg_height, bg_width, 3)) * 255 pil_img = Image.fromarray(np.uint8(pure_bg)) draw = ImageDraw.Draw(pil_img) # Draw text in the center of bg text_x = int((bg_width - word_width) / 2) text_y = int((bg_height - word_height) / 2) word_color = self.get_word_color() if apply(self.cfg.random_space): text_x, text_y, word_width, word_height = self.draw_text_with_random_space( draw, font, word, word_color, bg_width, bg_height, pure_bg) else: if apply(self.cfg.texture) and not self.fonts_by_image: pure_bg = Image.new('RGBA', (bg_width, bg_height), (255, 255, 255, 255)) pil_img = Image.new('RGBA', (bg_width, bg_height), (255, 255, 255, 0)) draw = ImageDraw.Draw(pil_img) _ = self.draw_text_wrapper(draw, word, text_x - offset[0], text_y - offset[1], font, word_color, font2, pil_img) pil_img = self.texture.apply_cloud_texture(pure_bg, pil_img) else: if self.fonts_by_image: pil_img = self.draw_text_wrapper(draw, word, text_x - offset[0], text_y - offset[1], font, word_color, font2, pure_bg) else: _ = self.draw_text_wrapper(draw, word, text_x - offset[0], text_y - offset[1], font, word_color, font2, pure_bg) # draw.text((text_x - offset[0], text_y - offset[1]), word, fill=word_color, font=font) np_img = np.array(pil_img).astype(np.float32) text_box_pnts = [[text_x, text_y], [text_x + word_width, text_y], [text_x + word_width, text_y + word_height], [text_x, text_y + word_height]] return np_img, text_box_pnts, word_color
def gen_img(self, img_index): # print(0, time.time() - self.start) word, font, word_size, font2, font_size = self.pick_font(img_index) # print(1, time.time() - self.start) self.dmsg("after pick font") # Background's height should much larger than raw word image's height, # to make sure we can crop full word image after apply perspective bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) word_img, text_box_pnts, word_color = self.draw_text_on_bg( word, font, bg, font2, font_size) self.dmsg("After draw_text_on_bg") # print(2, time.time() - self.start) if apply(self.cfg.crop): text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) # print(3, time.time() - self.start) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts) self.dmsg("After draw line") # print(4, time.time() - self.start) if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) # print(5, time.time() - self.start) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply( word_img, text_box_pnts, word_color) self.dmsg("After remapping") # print(6, time.time() - self.start) if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) # print(7, time.time() - self.start) word_img = self.mix_seamless_bg(word_img, bg) if apply(self.cfg.extra_words): word_img = self.draw_extra_random_word( word_img, text_box_pnts, img_index) self.dmsg("After add extra words") word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=self.cfg.perspective_transform.max_x, max_y=self.cfg.perspective_transform.max_y, max_z=self.cfg.perspective_transform.max_z, gpu=self.gpu) self.dmsg("After perspective transform") # print(8, time.time() - self.start) if self.debug: _, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) word_img = draw_bbox(word_img, crop_bbox, (255, 0, 0)) else: word_img, crop_bbox = self.crop_img( word_img, text_box_pnts_transformed) self.dmsg("After crop_img") # print(9, time.time() - self.start) if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") # print(10, time.time() - self.start) if apply(self.cfg.blur): word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") word_img = np.clip(word_img, 0., 255.) # print(11, time.time() - self.start) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") # print(12, time.time() - self.start) if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") # print(13, time.time() - self.start) if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") return word_img, word
def draw_text_on_bg(self, word, font, bg, font2, font_size): """ Draw word in the center of background :param word: word to draw :param font: font to draw word :param bg: background numpy image :return: np_img: word image text_box_pnts: left-top, right-top, right-bottom, left-bottom """ bg_height = bg.shape[0] bg_width = bg.shape[1] word_size = self.get_word_size(font, word, font_size) word_height = word_size[1] word_width = word_size[0] offset = (0, 0) if self.fonts_by_image else font.getoffset(word) pure_bg = np.ones((bg_height, bg_width, 3)) * 255 pil_img = Image.fromarray(np.uint8(pure_bg)) draw = ImageDraw.Draw(pil_img) # print("pure_bg", pure_bg.shape) # Draw text in the center of bg text_x = int((bg_width - word_width) / 2) text_y = int((bg_height - word_height) / 2) word_color = self.get_word_color() # cv2.imwrite("/media/ruijun/新加卷/workspace/text_renderer/debug/debug.png", np.array(pil_img).astype(np.float32)) # print(self.cfg) if apply(self.cfg.random_space): # print(time.time() - self.start, "bingo") text_x, text_y, word_width, word_height, pil_img_ = self.draw_text_with_random_space(draw, font, word, word_color, bg_width, bg_height, pure_bg) else: if apply(self.cfg.texture): # print(time.time() - self.start, "aluba") pure_bg = Image.new( 'RGBA', (bg_width, bg_height), (255, 255, 255, 255)) pil_img = Image.new( 'RGBA', (bg_width, bg_height), (255, 255, 255, 0)) draw = ImageDraw.Draw(pil_img) pil_img_ = self.draw_text_wrapper( draw, word, text_x - offset[0], text_y - offset[1], font, word_color, font2, pure_bg) pil_img = self.texture.apply_cloud_texture(pure_bg, pil_img) else: # print(time.time() - self.start, "axiba") pil_img_ = self.draw_text_wrapper( draw, word, text_x - offset[0], text_y - offset[1], font, word_color, font2, pure_bg) # draw.text((text_x - offset[0], text_y - offset[1]), word, fill=word_color, font=font) # cv2.imwrite("/media/ruijun/新加卷/workspace/text_renderer/debug/debug1.png", np.array(pil_img).astype(np.float32)) if self.fonts_by_image: np_img = np.array(pil_img_).astype(np.float32) else: np_img = np.array(pil_img).astype(np.float32) text_box_pnts = [ [text_x, text_y], [text_x + word_width, text_y], [text_x + word_width, text_y + word_height], [text_x, text_y + word_height] ] return np_img, text_box_pnts, word_color
def gen_img(self, img_index): word, font, word_size = self.pick_font(img_index) self.dmsg("after pick font") #_________________________________Change_Spaces______________________________________________________ word = self.gen_word() r = random.randint(0, 3) if len(word) == 8: pass else: if r == 0: word += ' ' * rnd.randint(1, 16) else: pass # Background's height should much larger than raw word image's height, # to make sure we can crop full word image after apply perspective bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) word_img, text_box_pnts, word_color, piece_widths = self.draw_text_on_bg( word, font, bg) self.dmsg("After draw_text_on_bg") if True: text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("After draw line") #if self.debug: #___________________________________Change________________________________________________________ if True: word_img = draw_box(word_img, text_box_pnts, (0, 0, 0)) # Drawing boxes x_init, y = text_box_pnts[0][0], text_box_pnts[0][1] w_init, h = text_box_pnts[2][0] - x_init, text_box_pnts[2][1] - y x = x_init #piece_widths[-1] = w_init for i in range(len(word)): w = piece_widths[i] + x_init bbox_points = [x, y, w, h] word_img = draw_bbox(word_img, bbox_points, (0, 0, 0)) x = w if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply( word_img, text_box_pnts, word_color) self.dmsg("After remapping") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=self.cfg.perspective_transform.max_x, max_y=self.cfg.perspective_transform.max_y, max_z=self.cfg.perspective_transform.max_z, gpu=self.gpu) self.dmsg("After perspective transform") if self.debug: _, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) word_img = draw_bbox(word_img, crop_bbox, (255, 0, 0)) else: word_img, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) self.dmsg("After crop_img") if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("After prydown") word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") return word_img, word