Skip to content

Commit

Permalink
add disable-font-border
Browse files Browse the repository at this point in the history
  • Loading branch information
zyddnys committed Jul 15, 2024
1 parent 37c209c commit 0653847
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
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('--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')
Expand Down
13 changes: 9 additions & 4 deletions manga_translator/rendering/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -181,12 +186,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 = '', 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

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, 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)
13 changes: 10 additions & 3 deletions manga_translator/rendering/text_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,19 @@ 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]
fg[:,:,1] = color[1]
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]
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions manga_translator/rendering/text_render_eng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
3 changes: 1 addition & 2 deletions manga_translator/translators/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down

0 comments on commit 0653847

Please sign in to comment.