Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translating PDF #5

Open
friki67 opened this issue Oct 23, 2024 · 1 comment
Open

Translating PDF #5

friki67 opened this issue Oct 23, 2024 · 1 comment

Comments

@friki67
Copy link

friki67 commented Oct 23, 2024

Hello! This is working for docx documents. But, when trying to translate a PDF document i got this trace in console and traceback in django

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/translator/

Django Version: 4.2.4
Python Version: 3.10.12
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'application']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/home/miguel/env/llm-translation/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/miguel/env/llm-translation/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/miguel/git/open_source_translation_tool/application/views.py", line 109, in upload_document
    translate_document(
  File "/home/miguel/git/open_source_translation_tool/src/translation_app.py", line 115, in translate_document
    save_translated_doc(translated_document, output_file, doc_type)
  File "/home/miguel/git/open_source_translation_tool/src/save_document.py", line 37, in save_translated_doc
    overwrite_text_on_images(document)
  File "/home/miguel/git/open_source_translation_tool/src/save_document.py", line 91, in overwrite_text_on_images
    img = inpaint_algorithm(
  File "/home/miguel/git/open_source_translation_tool/src/save_document.py", line 163, in inpaint_algorithm
    image = write_text_on_rectangle(
  File "/home/miguel/git/open_source_translation_tool/src/save_document.py", line 200, in write_text_on_rectangle
    font = ImageFont.truetype(FONT_FAMILY, size=FONT_SIZE)
  File "/home/miguel/env/llm-translation/lib/python3.10/site-packages/PIL/ImageFont.py", line 996, in truetype
    return freetype(font)
  File "/home/miguel/env/llm-translation/lib/python3.10/site-packages/PIL/ImageFont.py", line 993, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/home/miguel/env/llm-translation/lib/python3.10/site-packages/PIL/ImageFont.py", line 248, in __init__
    self.font = core.getfont(

Exception Type: OSError at /translator/
Exception Value: cannot open resource

Can you help me?

@friki67
Copy link
Author

friki67 commented Oct 23, 2024

Ok, I found it. In save_document.py, line 192 you have this

FONT_FAMILY = f"/home/ubuntu/LLM-translator/fonts/{font}"

And I changed it to this, so it worked.

FONT_FAMILY = f"{os.getcwd()}/fonts/{font}"

But now I'm facing this:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/translator/

Django Version: 4.2.4
Python Version: 3.10.12
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'application']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/home/miguel/env/llm-translation/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/miguel/env/llm-translation/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/miguel/git/open_source_translation_tool/application/views.py", line 109, in upload_document
    translate_document(
  File "/home/miguel/git/open_source_translation_tool/src/translation_app.py", line 115, in translate_document
    save_translated_doc(translated_document, output_file, doc_type)
  File "/home/miguel/git/open_source_translation_tool/src/save_document.py", line 37, in save_translated_doc
    overwrite_text_on_images(document)
  File "/home/miguel/git/open_source_translation_tool/src/save_document.py", line 91, in overwrite_text_on_images
    img = inpaint_algorithm(
  File "/home/miguel/git/open_source_translation_tool/src/save_document.py", line 163, in inpaint_algorithm
    image = write_text_on_rectangle(
  File "/home/miguel/git/open_source_translation_tool/src/save_document.py", line 205, in write_text_on_rectangle
    lines = text_wrap(text, font, max_width)
  File "/home/miguel/git/open_source_translation_tool/src/save_document.py", line 242, in text_wrap
    if font.getsize(text)[0] <= max_width:
  File "/home/miguel/env/llm-translation/lib/python3.10/site-packages/PIL/ImageFont.py", line 480, in getsize
    size, offset = self.font.getsize(text, "L", direction, features, language)

Exception Type: OSError at /translator/
Exception Value: invalid argument

Once again related to ImageFont.py. Now is trickier for me. Can you help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant