Skip to content

Commit

Permalink
Revert image upscaling for skipped images
Browse files Browse the repository at this point in the history
Moved the `if ctx.revert_upscaling` code-chunk to its own function, which is called during all return statements within the `_translate` function.
  • Loading branch information
SamuelWN authored May 7, 2024
1 parent b5f91d9 commit 940de0a
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions manga_translator/manga_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ async def _translate(self, ctx: Context) -> Context:
await self._report_progress('skip-no-regions', True)
# If no text was found result is intermediate image product
ctx.result = ctx.upscaled
return ctx
return await self._revert_upscale(ctx)

if self.verbose:
img_bbox_raw = np.copy(ctx.img_rgb)
for txtln in ctx.textlines:
Expand All @@ -396,7 +397,7 @@ async def _translate(self, ctx: Context) -> Context:
await self._report_progress('skip-no-text', True)
# If no text was found result is intermediate image product
ctx.result = ctx.upscaled
return ctx
return await self._revert_upscale(ctx)

if ctx.skip_lang is not None :
skip_langs = ctx.skip_lang.split(',')
Expand All @@ -406,7 +407,7 @@ async def _translate(self, ctx: Context) -> Context:
print('skip due to', source_language, 'in', skip_langs)
await self._report_progress('finished', True)
ctx.result = ctx.upscaled
return ctx
return await self._revert_upscale(ctx)

# -- Textline merge
await self._report_progress('textline_merge')
Expand All @@ -425,11 +426,11 @@ async def _translate(self, ctx: Context) -> Context:
if not ctx.text_regions:
await self._report_progress('error-translating', True)
ctx.result = ctx.upscaled
return ctx
return await self._revert_upscale(ctx)
elif ctx.text_regions == 'cancel':
await self._report_progress('cancelled', True)
ctx.result = ctx.upscaled
return ctx
return await self._revert_upscale(ctx)

# -- Mask refinement
# (Delayed to take advantage of the region filtering done after ocr and translation)
Expand Down Expand Up @@ -459,6 +460,11 @@ async def _translate(self, ctx: Context) -> Context:
await self._report_progress('finished', True)
ctx.result = dump_image(ctx.input, ctx.img_rendered, ctx.img_alpha)

return await self._revert_upscale(ctx)

# If `revert_upscaling` is True, revert to input size
# Else leave `ctx` as-is
async def _revert_upscale(self, ctx: Context):
if ctx.revert_upscaling:
await self._report_progress('downscaling')
ctx.result = ctx.result.resize(ctx.input.size)
Expand Down

0 comments on commit 940de0a

Please sign in to comment.