Translate Image
Translator
The Translator class is the core component of the lara-sdk, designed for translating image. It must be initialized with authentication credentials.
from lara_sdk import Translator, Credentials
LARA_ACCESS_KEY_ID = "your-access-key-id"
LARA_ACCESS_KEY_SECRET = "your-access-key-secret"
# Initialization of the Translator class
credentials = Credentials(access_key_id=LARA_ACCESS_KEY_ID, access_key_secret=LARA_ACCESS_KEY_SECRET)
lara = Translator(credentials)import {Credentials, Translator} from "@translated/lara";
const LARA_ACCESS_KEY_ID: string = "your-access-key-id";
const LARA_ACCESS_KEY_SECRET: string = "your-access-key-secret";
// Initialization of the Translator class
const credentials = new Credentials(LARA_ACCESS_KEY_ID, LARA_ACCESS_KEY_SECRET)
const lara: Translator = new Translator(credentials);import com.translated.lara.Credentials;
import com.translated.lara.translator.Translator;
private static final String LARA_ACCESS_KEY_ID = "your-access-key-id";
private static final String LARA_ACCESS_KEY_SECRET = "your-access-key-secret";
// Initialization of the Translator class
Credentials credentials = new Credentials(LARA_ACCESS_KEY_ID, LARA_ACCESS_KEY_SECRET);
Translator lara = new Translator(credentials);<?php
require 'vendor/autoload.php';
use Lara\Translator;
use Lara\LaraCredentials;
$LARA_ACCESS_KEY_ID = "your-access-key-id";
$LARA_ACCESS_KEY_SECRET = "your-access-key-secret";
// Initialization of the Translator class
$credentials = new LaraCredentials($LARA_ACCESS_KEY_ID, $LARA_ACCESS_KEY_SECRET);
$lara = new Translator($credentials);import (
lara_sdk "github.com/translated/lara-go/lara" // alias "lara_sdk", can be changed or removed
)
const (
LARA_ACCESS_KEY_ID = "your-access-key-id"
LARA_ACCESS_KEY_SECRET = "your-access-key-secret"
)
credentials := lara_sdk.NewCredentials(LARA_ACCESS_KEY_ID, LARA_ACCESS_KEY_SECRET)
lara := lara_sdk.NewTranslator(credentials, nil)
import com.translated.lara.Credentials
import com.translated.lara.translator.Translator
private const val LARA_ACCESS_KEY_ID = "your-access-key-id"
private const val LARA_ACCESS_KEY_SECRET = "your-access-key-secret"
// Initialization of the Translator class
val credentials = Credentials(LARA_ACCESS_KEY_ID, LARA_ACCESS_KEY_SECRET)
val lara = Translator(credentials)string accessKeyId = "your-access-key-id";
string accessKeySecret = "your-access-key-secret";
var credentials = new Credentials(accessKeyId, accessKeySecret);
var lara = new Translator(credentials);let LARA_ACCESS_KEY_ID = "your-access-key-id"
let LARA_ACCESS_KEY_SECRET = "your-access-key-secret"
// Initialization of the Translator class
let credentials = Credentials(accessKeyId: LARA_ACCESS_KEY_ID, accessKeySecret: LARA_ACCESS_KEY_SECRET)
let lara = Translator(credentials: credentials)Translate
Translates image from a source language to a target language. It supports context-aware translations and adaptation to specific translation memories, it returns a translated image.
Request
sample_image_path = "sample_image.png"
translated_image = lara.images.translate(
source="en-US",
target="fr-FR",
image_path=sample_image_path,
text_removal="overlay"
)const res = await lara.images.translate('path/to/image.jpg', 'en-US', 'it-IT', {
adaptTo: ['mem_1_id', 'mem_2_id'],
glossaries: ['gls_1_id', 'gls_2_id'],
instructions: ['Be formal'],
style: 'fluid'
});File imageFile = new File("image.jpg");
ImageTranslateOptions options = new ImageTranslateOptions();
options.setAdaptTo("mem_1234...");
options.setGlossaries("gls_1234...");
options.setTextRemoval(ImageTextRemoval.OVERLAY);
InputStream translated = lara.images.translate(imageFile, "en-US", "it-IT", options);$sampleFilePath = __DIR__ . '/sample_image.png';
$sourceLang = "en";
$targetLang = "de";
$translatedStream = $lara->images->translate($sampleFilePath, $sourceLang, $targetLang, new ImageTranslationOptions([
'textRemoval' => 'overlay'
]));
$outputPath = __DIR__ . '/sample_image_translated.png';
$outputFile = fopen($outputPath, 'w');
stream_copy_to_stream($translatedStream, $outputFile);
fclose($outputFile);
fclose($translatedStream);// Sdk available soon
// Sdk available soon
val imageFile = File("image.jpg")
val options = ImageTranslateOptions().apply {
adaptTo = "mem_1234..."
glossaries = "gls_1234..."
textRemoval = ImageTextRemoval.OVERLAY
}
val translated: InputStream = lara.images.translate(imageFile, "en-US", "it-IT", options)var sampleFilePath = Path.Combine(Directory.GetCurrentDirectory(), "textimage.png");
var sourceLang = "en";
var targetLang = "de";
var translatedStream = await lara.Images.Translate(sampleFilePath, sourceLang, targetLang, options);
var outputPath = Path.Combine(Directory.GetCurrentDirectory(), "sample_image_translated.png");
await using (var fileStream = File.Create(outputPath))
{
await translatedStream.CopyToAsync(fileStream);
}
// Create a MultipartFile from the image data
let file = MultipartFile(filename: "sample_image.png", data: imageData)
let translatedImageData = try await lara.images.translate(
file: file,
source: sourceLang,
target: targetLang,
options: ImageTranslationOptions(textRemoval: .overlay)
)
// Save the translated image
let outputPath = "./sample_image_translated.png"
try translatedImageData.write(to: URL(fileURLWithPath: outputPath))Here follows the basic fields for the translate method:
Field | Type | Required | Default | Description |
|---|---|---|---|---|
file / imagePath | File / String | Yes | The input image/the path to input image to translate. | |
source | String | No | Autodetected | The source language code (e.g., "en-EN" for English). If not specified, the system will attempt to detect it automatically. |
target | String | Yes | The target language code (e.g., "it-IT" for Italian). This specifies the language you want the text translated into. | |
options | ImageTranslationOptions | No | See the table below for details. |
Several options are available to customize the behavior of the translate method:
Field | Type | Required | Default | Description |
|---|---|---|---|---|
adaptTo | String[] | No | Default is all Memories on your account | A list of translation memory IDs for adapting the translation. |
glossaries | String[] | No | A list of glossary IDs. | |
noTrace | Boolean | No | False | If set to True, source content and its translation will not be saved on our system. (AKA Incognito mode) |
style | String | No |
| The style to apply to the translation. Available values:
|
textRemoval | String | No |
| Customize how the original text is removed from the image. |
Translate text image
Translates the text inside an image from a source language to a target language. It supports context-aware translations and adaptation to specific translation memories, it returns the original and the translated text.
Request
sample_image_path = "sample_image.png"
text_results = lara.images.translate_text(
source="en-US",
target="es-ES",
image_path=sample_image_path
)const res = await lara.images.translateText('path/to/image.jpg', 'en-US', 'it-IT', {
adaptTo: ['mem_1_id', 'mem_2_id'],
glossaries: ['gls_1_id', 'gls_2_id'],
style: 'fluid'
});File imageFile = new File("image.jpg");
ImageTextTranslateOptions options = new ImageTextTranslateOptions();
options.setAdaptTo("mem_1234...");
options.setGlossaries("gls_1234...");
ImageTextResult result = lara.images.translateText(imageFile, "en-US", "it-IT", options);$sampleFilePath = __DIR__ . '/sample_image.png';
$sourceLang = "en";
$targetLang = "de";
$results = $lara->images->translateText($sampleFilePath, $sourceLang, $targetLang, new ImageTextTranslationOptions([
'adaptTo' => ['mem_1A2b3C4d5E6f7G8h9I0jKl'], // Replace with actual memory IDs
'glossaries' => ['gls_1A2b3C4d5E6f7G8h9I0jKl'], // Replace with actual glossary IDs
'style' => 'faithful'
]));
foreach ($results as $index => $result) {
echo "\nText Block " . ($index + 1) . ":\n";
echo "Original: " . $result->getText() . "\n";
echo "Translated: " . $result->getTranslation() . "\n";
}// Sdk available soon
val imageFile = File("image.jpg")
val options = ImageTextTranslateOptions().apply {
adaptTo = "mem_1234..."
glossaries = "gls_1234..."
}
val result: ImageTextResult = lara.images.translateText(imageFile, "en-US", "it-IT", options)var sampleFilePath = Path.Combine(Directory.GetCurrentDirectory(), "textimage.png");
var sourceLang = "en";
var targetLang = "de";
var options = new ImageTextTranslateOptions
{
AdaptTo = new[] { "mem_1A2b3C4d5E6f7G8h9I0jKl" }, // Replace with actual memory IDs
Glossaries = new[] { "gls_1A2b3C4d5E6f7G8h9I0jKl" }, // Replace with actual glossary IDs
Style = TranslationStyle.Faithful
};
var results = await lara.Images.TranslateText(sampleFilePath, sourceLang, targetLang, options);
for (int i = 0; i < results.Paragraphs.Length; i++)
{
var paragraph = results.Paragraphs[i];
Console.WriteLine($"\nText Block {i + 1}:");
Console.WriteLine($"Original: {paragraph.Text}");
Console.WriteLine($"Translated: {paragraph.Translation}");
}// Create another MultipartFile for text extraction
let file3 = MultipartFile(filename: "sample_image.png", data: imageData)
let textOptions = ImageTextTranslationOptions(
adaptTo: ["mem_1A2b3C4d5E6f7G8h9I0jKl"], // Replace with actual memory IDs
glossaries: ["gls_1A2b3C4d5E6f7G8h9I0jKl"], // Replace with actual glossary IDs
style: .faithful
)
let results = try await lara.images.translateText(
file: file3,
source: sourceLang,
target: targetLang,
options: textOptions
)
// Display each text block and its translation
for (index, paragraph) in results.paragraphs.enumerated() {
print("\nText Block \(index + 1):")
print("Original: \(paragraph.text)")
print("Translated: \(paragraph.translation)")
}Here follows the basic fields for the translateText method:
Field | Type | Required | Default | Description |
|---|---|---|---|---|
file / imagePath | File / String | Yes | The input image/the path to input image to translate. | |
source | String | No | Autodetected | The source language code (e.g., "en-EN" for English). If not specified, the system will attempt to detect it automatically. |
target | String | Yes | The target language code (e.g., "it-IT" for Italian). This specifies the language you want the text translated into. | |
options | ImageTextTranslationOptions | No | See the table below for details. |
Several options are available to customize the behavior of the translateText method:
Field | Type | Required | Default | Description |
|---|---|---|---|---|
adaptTo | String[] | No | Default is all Memories on your account | A list of translation memory IDs for adapting the translation. |
glossaries | String[] | No | A list of glossary IDs. | |
noTrace | Boolean | No | False | If set to True, source content and its translation will not be saved on our system. (AKA Incognito mode) |
style | String | No |
| The style to apply to the translation. Available values:
|
Supported languages
Billing
Details are available in the pricing page
Updated 8 days ago
