zyddnys / manga-image-translator

Translate manga/image 一键翻译各类图片内文字 https://cotrans.touhou.ai/
https://cotrans.touhou.ai/
GNU General Public License v3.0
5.12k stars 528 forks source link

[Bug]: FT_Exception: FT_Exception: (cannot open resource) #375

Closed bubobu closed 1 year ago

bubobu commented 1 year ago

Issue

I set font path by absolute, content root, but i cant solve this problem. and reinstall project, freetype. I also refer tio #57 .

Command Line Arguments

python -m manga_translator -v --mode batch --translator=google -l ENG -i C:\Users\user\Downloads\dist\hitomi_downloader_GUI\hitomi_downloaded\23

Console logs

after ocr

[batch] Running text translation
[GoogleTranslator] Translating into English
[batch] Running mask refinement
[mask]: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 28/28 [00:00<00:00, 147.79it/s]
[LamaMPEInpainter] Inpainting resolution: 1280x1816
[batch] Running rendering
ERROR: [batch] FT_Exception: FT_Exception:  (cannot open resource)
Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\__main__.py", line 66, in <module>
    loop.run_until_complete(dispatch(args))
  File "C:\Python\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\__main__.py", line 35, in dispatch
    await translator.translate_path(args.input, dest, args_dict)
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\manga_translator.py", line 154, in translate_path
    if await self._translate_file(file_path, output_dest, params):
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\manga_translator.py", line 173, in _translate_file
    translation_dict = await self.translate(img, params)
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\manga_translator.py", line 249, in translate
    return await self._translate(ctx)
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\manga_translator.py", line 361, in _translate
    ctx.img_rendered = await self._run_text_rendering(ctx)
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\manga_translator.py", line 514, in _run_text_rendering
    output = await dispatch_rendering(ctx.img_inpainted, ctx.text_regions, ctx.font_path, ctx.font_size, ctx.font_size_offset,
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\rendering\__init__.py", line 101, in dispatch
    text_render.set_font(font_path)
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\rendering\text_render.py", line 132, in set_font
    FONT_SELECTION = [get_cached_font(p) for p in selection]
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\rendering\text_render.py", line 132, in <listcomp>
    FONT_SELECTION = [get_cached_font(p) for p in selection]
  File "C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\rendering\text_render.py", line 123, in get_cached_font
    font_cache[path] = freetype.Face(path)
  File "C:\Python\lib\site-packages\freetype\__init__.py", line 1192, in __init__
    raise FT_Exception(error)
freetype.ft_errors.FT_Exception: FT_Exception:  (cannot open resource)
zyddnys commented 1 year ago

can you change line 121 to 124 of C:\Users\user\PycharmProjects\manga-image-translator\manga_translator\rendering\text_render.py to

def get_cached_font(path: str) -> freetype.Face:
    if not font_cache.get(path):
        print('loading font from', path)
        font_cache[path] = freetype.Face(path)
    return font_cache[path]

so we can be sure that the files do exist?

Omicronlawful commented 1 year ago

Why aren’t you just using system fonts instead?

zyddnys commented 1 year ago

Why aren’t you just using system fonts instead?

some system does not have CJK fonts

Omicronlawful commented 1 year ago

If I got that right cfg fonts are Chinese, Japanese etc right. Of course you need to have the font installed before using them, but when you want to translate to one of these languages your pc is most likely able to. I played around with pango/Cairo(pdf output) and they require the font family instead and heave no easy way to set the font from a ttf file. I think python can install fonts with fonttools

bubobu commented 1 year ago

I resolve this problem. by changing font location. so it can perceive font file.