批量“翻译”图像

问题描述 投票:0回答:1

我正在尝试找到一种批量翻译图像的简单方法。我想玩一款日本游戏,游戏中的几乎所有内容都保存为图像,包括按钮、对话等。

翻译不必完美无缺,只要在带有英文翻译的图像顶部有一个黑条就足够了。

我遇到了另一篇文章与我有同样的问题,但是该文章上的解决方案不起作用。

这个想法是使用 google-cloud-translate 库并有一个小的 python 脚本来转换图片。代码如下:

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)

这是我收到的错误消息:

   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.

所以我继续在 Google Cloud 上注册并创建了一个项目。这是我的第一个问题:我需要哪个 API?我想是“云翻译 API”,对吗?然后我只需要创建一个具有该权限的服务帐户并锁定凭据? 如果有人可以提供快速的分步教程,我将非常感激。

image google-translate python google-cloud-platform
1个回答
0
投票

这里是 Google Translate API 入门Python 中的设置

但是您也可以通过例如使用无头网络浏览器Puppeteer 批量翻译图像。

© www.soinside.com 2019 - 2024. All rights reserved.