diff --git a/manga_translator/args.py b/manga_translator/args.py index b4e65d5d2..710dea9df 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('--denoise-sigma', default=30, type=int, help='Used by colorizer and affects color strength, range from 0 to 255 (default 30). -1 turns it off.') parser.add_argument('--mask-dilation-offset', default=0, type=int, help='By how much to extend the text mask to remove left-over text pixels of the original image.') +parser.add_argument('--disable-font-border', action='store_true', help='Disable font border') parser.add_argument('--font-size', default=None, type=int, help='Use fixed font size for rendering') 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') diff --git a/manga_translator/rendering/__init__.py b/manga_translator/rendering/__init__.py index 111ef0dd4..520b45356 100644 --- a/manga_translator/rendering/__init__.py +++ b/manga_translator/rendering/__init__.py @@ -98,7 +98,8 @@ async def dispatch( font_size_minimum: int = 0, hyphenate: bool = True, render_mask: np.ndarray = None, - line_spacing: int = None + line_spacing: int = None, + disable_font_border: bool = False ) -> np.ndarray: text_render.set_font(font_path) @@ -114,7 +115,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, line_spacing) + img = render(img, region, dst_points, hyphenate, line_spacing, disable_font_border) return img def render( @@ -123,10 +124,14 @@ def render( dst_points, hyphenate, line_spacing, + disable_font_border ): fg, bg = region.get_font_colors() fg, bg = fg_bg_compare(fg, bg) + if disable_font_border : + bg = None + middle_pts = (dst_points[:, [1, 2, 3, 0]] + dst_points) / 2 norm_h = np.linalg.norm(middle_pts[:, 1] - middle_pts[:, 3], axis=1) norm_v = np.linalg.norm(middle_pts[:, 2] - middle_pts[:, 0], axis=1) @@ -181,7 +186,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 = '', line_spacing: int = 0) -> 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, disable_font_border: bool = False) -> np.ndarray: if len(text_regions) == 0: return img_canvas @@ -189,4 +194,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, line_spacing=line_spacing, 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,disable_font_border=disable_font_border) diff --git a/manga_translator/rendering/text_render.py b/manga_translator/rendering/text_render.py index a3f2797e1..fa6fcd63d 100644 --- a/manga_translator/rendering/text_render.py +++ b/manga_translator/rendering/text_render.py @@ -122,7 +122,10 @@ def add_color(bw_char_map, color, stroke_char_map, stroke_color): # plt.show() # since bg rect is always larger than fg rect, we can just use the bg rect - x, y, w, h = cv2.boundingRect(stroke_char_map) + if stroke_color is None : + x, y, w, h = cv2.boundingRect(bw_char_map) + else : + x, y, w, h = cv2.boundingRect(stroke_char_map) fg = np.zeros((h, w, 4), dtype = np.uint8) fg[:,:,0] = color[0] @@ -130,6 +133,8 @@ def add_color(bw_char_map, color, stroke_char_map, stroke_color): fg[:,:,2] = color[2] fg[:,:,3] = bw_char_map[y:y+h, x:x+w] + if stroke_color is None : + stroke_color = color bg = np.zeros((stroke_char_map.shape[0], stroke_char_map.shape[1], 4), dtype = np.uint8) bg[:,:,0] = stroke_color[0] bg[:,:,1] = stroke_color[1] @@ -340,9 +345,11 @@ def put_text_vertical(font_size: int, text: str, h: int, alignment: str, fg: Tup # colorize canvas_border = np.clip(canvas_border, 0, 255) line_box = add_color(canvas_text, fg, canvas_border, bg) - # rect - x, y, w, h = cv2.boundingRect(canvas_border) + if bg is None : + x, y, w, h = cv2.boundingRect(canvas_text) + else : + x, y, w, h = cv2.boundingRect(canvas_border) return line_box[y:y+h, x:x+w] def select_hyphenator(lang: str): diff --git a/manga_translator/rendering/text_render_eng.py b/manga_translator/rendering/text_render_eng.py index 9c313533d..331ea4f6c 100644 --- a/manga_translator/rendering/text_render_eng.py +++ b/manga_translator/rendering/text_render_eng.py @@ -345,6 +345,7 @@ def render_textblock_list_eng( ballonarea_thresh: float = 2, downscale_constraint: float = 0.7, original_img: np.ndarray = None, + disable_font_border: bool = False ) -> np.ndarray: r""" diff --git a/manga_translator/translators/chatgpt.py b/manga_translator/translators/chatgpt.py index 3e3dd5d72..170eab4cd 100644 --- a/manga_translator/translators/chatgpt.py +++ b/manga_translator/translators/chatgpt.py @@ -54,8 +54,7 @@ class GPT3Translator(CommonTranslator): def __init__(self, check_openai_key = True): super().__init__() - self.client = openai.AsyncOpenAI() - self.client.api_key = openai.api_key or OPENAI_API_KEY + self.client = openai.AsyncOpenAI(api_key = openai.api_key or OPENAI_API_KEY) self.client.base_url = OPENAI_API_BASE if not self.client.api_key and check_openai_key: raise MissingAPIKeyException('Please set the OPENAI_API_KEY environment variable before using the chatgpt translator.')