Skip to content

Commit

Permalink
Add --line-spacing and reduce its default
Browse files Browse the repository at this point in the history
  • Loading branch information
thatDudo committed Nov 3, 2023
1 parent 1d9247e commit cc490e6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 17 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions manga_translator/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions manga_translator/manga_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions manga_translator/rendering/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -113,14 +114,15 @@ 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(
img,
region: TextBlock,
dst_points,
hyphenate,
line_spacing,
):
fg, bg = region.get_font_colors()
fg, bg = fg_bg_compare(fg, bg)
Expand All @@ -142,6 +144,7 @@ def render(
bg,
region.target_lang,
hyphenate,
line_spacing,
)
else:
temp_box = text_render.put_text_vertical(
Expand All @@ -151,6 +154,7 @@ def render(
region.alignment,
fg,
bg,
line_spacing,
)
h, w, _ = temp_box.shape
r_temp = w / h
Expand All @@ -177,12 +181,12 @@ 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

if not font_path:
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)
8 changes: 4 additions & 4 deletions manga_translator/rendering/text_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions manga_translator/rendering/text_render_eng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit cc490e6

Please sign in to comment.