1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] bulk "translate" images

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024 às 23:02.

  1. Stack

    Stack Membro Participativo

    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:

    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:

    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.

    Continue reading...

Compartilhe esta Página