trying to use this python code i found online to translate images with Google Translate in bulk but keep getting the "No module named 'google' error"

125 views Asked by At

i'm trying to use this code i found online to translate images with Google Translate in bulk

# 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.
# """c

input_dir = "D:\e\homework\manga doujins and hentai\[Shuuki] Onechan ga wakan nai! 1"
output_dir = "D:\e\homework\manga doujins and hentai\[Shuuki] Onechan ga wakan nai! 1\\translated"
target_language = "french"

# Create a Google Translate client.
from google.cloud import translate
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)

but I keep getting the ModuleNotFoundError: No module named 'google' error

I wanted for each image to be uploaded to google translate, translate it to another language, downloaded the translated image, and do so for every other image in the directory

1

There are 1 answers

0
user16171413 On

This is a warning on PyPi:

The google-cloud Python package is deprecated. On June 18, 2018, this package will no longer install any other packages. Please install the product-specific google-cloud-* packages needed for your application.

In your case, it appears you want to use the translate package, so you need to look at https://pypi.org/project/google-cloud-translate/. So create a venv and pip install google-cloud-translate in your venv. Remember to use a supported python version as google-cloud-translate currently requires Python >=3.7.