From cc490e6403d078b7ba3f22003775005ff83aadc2 Mon Sep 17 00:00:00 2001 From: thatDudo Date: Fri, 3 Nov 2023 16:08:03 +0100 Subject: [PATCH] Add --line-spacing and reduce its default --- README.md | 6 ++++-- README_CN.md | 6 ++++-- manga_translator/args.py | 1 + manga_translator/manga_translator.py | 4 ++-- manga_translator/rendering/__init__.py | 10 +++++++--- manga_translator/rendering/text_render.py | 8 ++++---- manga_translator/rendering/text_render_eng.py | 8 ++++---- 7 files changed, 26 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f4c1a0f22..6b33861d5 100644 --- a/README.md +++ b/README.md @@ -382,7 +382,7 @@ THA: Thai image folder if using batch mode -o, --dest DEST Path to the destination folder for translated images in batch mode --l, --target-lang {CHS,CHT,CSY,NLD,ENG,FRA,DEU,HUN,ITA,JPN,KOR,PLK,PTB,ROM,RUS,ESP,TRK,UKR,VIN,ARA,CNR,SRP,HRV,THA} +-l, --target-lang {CHS,CHT,CSY,NLD,ENG,FRA,DEU,HUN,ITA,JPN,KOR,PLK,PTB,ROM,RUS,ESP,TRK,UKR,VIN,ARA,CNR,SRP,HRV,THA,IND} Destination language -v, --verbose Print debug info and save intermediate images in result folder @@ -448,6 +448,8 @@ THA: Thai model. Use hex string without the "#" such as FFFFFF for a white foreground or FFFFFF:000000 to also have a black background around the text. +--line-spacing LINE_SPACING Line spacing is font_size * this value. Default is 0.01 + for horizontal text and 0.2 for vertical. --force-horizontal Force text to be rendered horizontally --force-vertical Force text to be rendered vertically --align-left Align rendered text left @@ -461,7 +463,7 @@ THA: Thai additional typesetting. Ignores some other argument options --gpt-config GPT_CONFIG Path to GPT config file, more info in README ---mtpe Turn on/off machine translation post editing (MTPE) on +--use-mtpe Turn on/off machine translation post editing (MTPE) on the command line (works only on linux right now) --save-text Save extracted text and translations into a text file. --save-text-file SAVE_TEXT_FILE Like --save-text but with a specified file path. diff --git a/README_CN.md b/README_CN.md index e83fd761f..587413491 100644 --- a/README_CN.md +++ b/README_CN.md @@ -114,7 +114,7 @@ THA: Thai image folder if using batch mode -o, --dest DEST Path to the destination folder for translated images in batch mode --l, --target-lang {CHS,CHT,CSY,NLD,ENG,FRA,DEU,HUN,ITA,JPN,KOR,PLK,PTB,ROM,RUS,ESP,TRK,UKR,VIN,ARA,CNR,SRP,HRV,THA} +-l, --target-lang {CHS,CHT,CSY,NLD,ENG,FRA,DEU,HUN,ITA,JPN,KOR,PLK,PTB,ROM,RUS,ESP,TRK,UKR,VIN,ARA,CNR,SRP,HRV,THA,IND} Destination language -v, --verbose Print debug info and save intermediate images in result folder @@ -180,6 +180,8 @@ THA: Thai model. Use hex string without the "#" such as FFFFFF for a white foreground or FFFFFF:000000 to also have a black background around the text. +--line-spacing LINE_SPACING Line spacing is font_size * this value. Default is 0.01 + for horizontal text and 0.2 for vertical. --force-horizontal Force text to be rendered horizontally --force-vertical Force text to be rendered vertically --align-left Align rendered text left @@ -193,7 +195,7 @@ THA: Thai additional typesetting. Ignores some other argument options --gpt-config GPT_CONFIG Path to GPT config file, more info in README ---mtpe Turn on/off machine translation post editing (MTPE) on +--use-mtpe Turn on/off machine translation post editing (MTPE) on the command line (works only on linux right now) --save-text Save extracted text and translations into a text file. --save-text-file SAVE_TEXT_FILE Like --save-text but with a specified file path. diff --git a/manga_translator/args.py b/manga_translator/args.py index 3bff99454..46de63ca4 100644 --- a/manga_translator/args.py +++ b/manga_translator/args.py @@ -134,6 +134,7 @@ def _format_action_invocation(self, action: argparse.Action) -> str: parser.add_argument('--font-size-offset', default=0, type=int, help='Offset font size by a given amount, positive number increase font size and vice versa') parser.add_argument('--font-size-minimum', default=-1, type=int, help='Minimum output font size. Default is image_sides_sum/200') parser.add_argument('--font-color', default=None, type=str, help='Overwrite the text fg/bg color detected by the OCR model. Use hex string without the "#" such as FFFFFF for a white foreground or FFFFFF:000000 to also have a black background around the text.') +parser.add_argument('--line-spacing', default=None, type=int, help='Line spacing is font_size * this value. Default is 0.01 for horizontal text and 0.2 for vertical.') g = parser.add_mutually_exclusive_group() g.add_argument('--force-horizontal', action='store_true', help='Force text to be rendered horizontally') diff --git a/manga_translator/manga_translator.py b/manga_translator/manga_translator.py index a0f67ee75..8930121ac 100644 --- a/manga_translator/manga_translator.py +++ b/manga_translator/manga_translator.py @@ -542,11 +542,11 @@ async def _run_text_rendering(self, ctx: Context): # manga2eng currently only supports horizontal left to right rendering elif ctx.renderer == 'manga2eng' and ctx.text_regions and LANGAUGE_ORIENTATION_PRESETS.get( ctx.text_regions[0].target_lang) == 'h': - output = await dispatch_eng_render(ctx.img_inpainted, ctx.img_rgb, ctx.text_regions, ctx.font_path) + output = await dispatch_eng_render(ctx.img_inpainted, ctx.img_rgb, ctx.text_regions, ctx.font_path, ctx.line_spacing) else: output = await dispatch_rendering(ctx.img_inpainted, ctx.text_regions, ctx.font_path, ctx.font_size, ctx.font_size_offset, - ctx.font_size_minimum, not ctx.no_hyphenation, ctx.render_mask) + ctx.font_size_minimum, not ctx.no_hyphenation, ctx.render_mask, ctx.line_spacing) return output def _result_path(self, path: str) -> str: diff --git a/manga_translator/rendering/__init__.py b/manga_translator/rendering/__init__.py index f8f878c4c..d088db334 100644 --- a/manga_translator/rendering/__init__.py +++ b/manga_translator/rendering/__init__.py @@ -98,6 +98,7 @@ async def dispatch( font_size_minimum: int = 0, hyphenate: bool = True, render_mask: np.ndarray = None, + line_spacing: int = None ) -> np.ndarray: text_render.set_font(font_path) @@ -113,7 +114,7 @@ async def dispatch( if render_mask is not None: # set render_mask to 1 for the region that is inside dst_points cv2.fillConvexPoly(render_mask, dst_points.astype(np.int32), 1) - img = render(img, region, dst_points, hyphenate) + img = render(img, region, dst_points, hyphenate, line_spacing) return img def render( @@ -121,6 +122,7 @@ def render( region: TextBlock, dst_points, hyphenate, + line_spacing, ): fg, bg = region.get_font_colors() fg, bg = fg_bg_compare(fg, bg) @@ -142,6 +144,7 @@ def render( bg, region.target_lang, hyphenate, + line_spacing, ) else: temp_box = text_render.put_text_vertical( @@ -151,6 +154,7 @@ def render( region.alignment, fg, bg, + line_spacing, ) h, w, _ = temp_box.shape r_temp = w / h @@ -177,7 +181,7 @@ def render( img[y:y+h, x:x+w] = np.clip((img[y:y+h, x:x+w].astype(np.float32) * (1 - mask_region) + canvas_region.astype(np.float32) * mask_region), 0, 255).astype(np.uint8) return img -async def dispatch_eng_render(img_canvas: np.ndarray, original_img: np.ndarray, text_regions: List[TextBlock], font_path: str = '') -> np.ndarray: +async def dispatch_eng_render(img_canvas: np.ndarray, original_img: np.ndarray, text_regions: List[TextBlock], font_path: str = '', line_spacing: int = 0) -> np.ndarray: if len(text_regions) == 0: return img_canvas @@ -185,4 +189,4 @@ async def dispatch_eng_render(img_canvas: np.ndarray, original_img: np.ndarray, font_path = os.path.join(BASE_PATH, 'fonts/comic shanns 2.ttf') text_render.set_font(font_path) - return render_textblock_list_eng(img_canvas, text_regions, size_tol=1.2, original_img=original_img, downscale_constraint=0.8) + return render_textblock_list_eng(img_canvas, text_regions, line_spacing=line_spacing, size_tol=1.2, original_img=original_img, downscale_constraint=0.8) diff --git a/manga_translator/rendering/text_render.py b/manga_translator/rendering/text_render.py index 196ce408d..3e53240b6 100644 --- a/manga_translator/rendering/text_render.py +++ b/manga_translator/rendering/text_render.py @@ -297,10 +297,10 @@ def put_char_vertical(font_size: int, cdpt: str, pen_l: Tuple[int, int], canvas_ canvas_border[pen_border[1]:pen_border[1]+bitmap_b.rows, pen_border[0]:pen_border[0]+bitmap_b.width] = cv2.add(canvas_border[pen_border[1]:pen_border[1]+bitmap_b.rows, pen_border[0]:pen_border[0]+bitmap_b.width], bitmap_border) return char_offset_y -def put_text_vertical(font_size: int, text: str, h: int, alignment: str, fg: Tuple[int, int, int], bg: Optional[Tuple[int, int, int]]): +def put_text_vertical(font_size: int, text: str, h: int, alignment: str, fg: Tuple[int, int, int], bg: Optional[Tuple[int, int, int]], line_spacing: int): text = compact_special_symbols(text) bg_size = int(max(font_size * 0.07, 1)) if bg is not None else 0 - spacing_x = int(font_size * 0.2) + spacing_x = int(font_size * (line_spacing or 0.2)) # make large canvas num_char_y = h // font_size @@ -659,10 +659,10 @@ def put_char_horizontal(font_size: int, cdpt: str, pen_l: Tuple[int, int], canva def put_text_horizontal(font_size: int, text: str, width: int, height: int, alignment: str, reversed_direction: bool, fg: Tuple[int, int, int], bg: Tuple[int, int, int], - lang: str = 'en_US', hyphenate: bool = True): + lang: str = 'en_US', hyphenate: bool = True, line_spacing: int = 0): text = compact_special_symbols(text) bg_size = int(max(font_size * 0.07, 1)) if bg is not None else 0 - spacing_y = int(font_size * 0.2) + spacing_y = int(font_size * (line_spacing or 0.01)) # calc # print(width) diff --git a/manga_translator/rendering/text_render_eng.py b/manga_translator/rendering/text_render_eng.py index c81d580f3..991ff7d9a 100644 --- a/manga_translator/rendering/text_render_eng.py +++ b/manga_translator/rendering/text_render_eng.py @@ -52,12 +52,13 @@ def render_lines( canvas_w: int, font_size: int, stroke_width: int, + line_spacing: int = 0.01, fg: Tuple[int] = (0, 0, 0), bg: Tuple[int] = (255, 255, 255)) -> Image.Image: # bg_size = int(max(font_size * 0.1, 1)) if bg is not None else 0 bg_size = stroke_width - spacing_y = int(font_size * 0.01) + spacing_y = int(font_size * (line_spacing or 0.01)) # make large canvas canvas_w = max([l.length for l in textlines]) + (font_size + bg_size) * 2 @@ -338,8 +339,7 @@ def render_textblock_list_eng( font_color = (0, 0, 0), stroke_color = (255, 255, 255), delimiter: str = ' ', - align_center=True, - line_spacing: float = 1.0, + line_spacing: int = 0.01, stroke_width: float = 0.1, size_tol: float = 1.0, ballonarea_thresh: float = 2, @@ -499,7 +499,7 @@ def update_enlarged_xyxy(region): line.pos_x -= canvas_x1 line.pos_y -= canvas_y1 - textlines_image = render_lines(textlines, canvas_h, canvas_w, font_size, sw, font_color, stroke_color) + textlines_image = render_lines(textlines, canvas_h, canvas_w, font_size, sw, line_spacing, font_color, stroke_color) rel_cx = ((canvas_x1 + canvas_x2) / 2 - rx) / resize_ratio rel_cy = ((canvas_y1 + canvas_y2) / 2 - ry + y_offset) / resize_ratio