import os
from google.cloud import translate_v3beta1 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 = "examplepathtothings"
output_dir = "examplepathtofinishedthings"
target_language = "en"
upload_and_translate(input_dir, output_dir, target_language)
Trying to use this script in order to translate a folders worth of jpg files which the google translate website will happily accept, not sure what to do from here.
AttributeError: ‘Client’ object has no attribute ‘translate_image’
And
AttributeError: module ‘google.cloud.translate_v3beta1’ has no attribute ‘Client’
are both shown when i attempt to run this, when this script has apparently worked for others in the past.
Not using a virtual environment, as I have read that it isn’t necessary. Dependencies have been re-installed as well.
Sojjer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.