OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

bulk "translate" images

  • Thread starter Thread starter Johnny Guides
  • Start date Start date
J

Johnny Guides

Guest
I'm trying to find an easy way to bulk-translate images. There's a Japanese game I wish to play and almost everything within the game is saved as an image, this includes buttons, dialogue, etc.

The translation doesn't have to be flawless, it's already enough if its just a black bar atop the image with the english translation.

I came across another post that had the same question as me, but the solution on that post did not work.

The idea was to use the google-cloud-translate library and have a small python script that converts the pictures. The code was as follows:

Code:
import os
from google.cloud import translate_v2 as translate

def upload_and_translate(input_dir, output_dir, target_language):
    """Uploads all images in a directory, translates them using Google Translate, and downloads the translated images to a specified output directory.

    Args:
        input_dir: The directory containing the images to be translated.
        output_dir: The directory to which the translated images will be downloaded.
        target_language: The target language for the translation.
    """

    # Create a Google Translate client.
    client = translate.Client()

    # Get a list of all the files in the input directory.
    files = os.listdir(input_dir)

    # Iterate over the files and upload them to Google Translate.
    for file in files:
        with open(os.path.join(input_dir, file), "rb") as f:
            # Upload the image to Google Translate.
            response = client.translate_image(
                f,
                target_language=target_language,
            )

            # Download the translated image.
            with open(os.path.join(output_dir, file), "wb") as f:
                f.write(response.translated_image)

# Example usage:
if __name__ == "__main__":
    input_dir = "path/to/input/directory"
    output_dir = "path/to/output/directory"
    target_language = "es"
    upload_and_translate(input_dir, output_dir, target_language)

This is the error message I got:

Code:
   raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)
google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.

So I went ahead, registered and created an project on Google Cloud. Here lies my first question: Which API will I need? I suppose the "Cloud Translation API", right? Then I just have to create an service account with rights on that and lock in the credentials? If somebody could give a quick step-by-step tutorial I would be very grateful.
<p>I'm trying to find an easy way to bulk-translate images. There's a Japanese game I wish to play and almost everything within the game is saved as an image, this includes buttons, dialogue, etc.</p>
<p>The translation doesn't have to be flawless, it's already enough if its just a black bar atop the image with the english translation.</p>
<p>I came across <a href="https://webapps.stackexchange.com/q/171216/330228">another post</a> that had the same question as me, but the <a href="https://webapps.stackexchange.com/a/173507/330228">solution on that post</a> did not work.</p>
<p>The idea was to use the google-cloud-translate library and have a small python script that converts the pictures. The code was as follows:</p>
<pre><code>import os
from google.cloud import translate_v2 as translate

def upload_and_translate(input_dir, output_dir, target_language):
"""Uploads all images in a directory, translates them using Google Translate, and downloads the translated images to a specified output directory.

Args:
input_dir: The directory containing the images to be translated.
output_dir: The directory to which the translated images will be downloaded.
target_language: The target language for the translation.
"""

# Create a Google Translate client.
client = translate.Client()

# Get a list of all the files in the input directory.
files = os.listdir(input_dir)

# Iterate over the files and upload them to Google Translate.
for file in files:
with open(os.path.join(input_dir, file), "rb") as f:
# Upload the image to Google Translate.
response = client.translate_image(
f,
target_language=target_language,
)

# Download the translated image.
with open(os.path.join(output_dir, file), "wb") as f:
f.write(response.translated_image)

# Example usage:
if __name__ == "__main__":
input_dir = "path/to/input/directory"
output_dir = "path/to/output/directory"
target_language = "es"
upload_and_translate(input_dir, output_dir, target_language)
</code></pre>
<p>This is the error message I got:</p>
<pre><code> raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)
google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.
</code></pre>
<p>So I went ahead, registered and created an project on Google Cloud. Here lies my first question: Which API will I need? I suppose the "Cloud Translation API", right? Then I just have to create an service account with rights on that and lock in the credentials?
If somebody could give a quick step-by-step tutorial I would be very grateful.</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top