Wolfram Language

External Services

Translate a Text in an Image

You can build a function that receives an image as input and translates the text in it to English.

In[1]:=
Click for copyable input
ImageTranslate[image_, rules___Rule] := ImageTranslate[image, Association@rules]; ImageTranslate[image_, args_] := Module[ {tr, gt, translatedText, to = Entity["Language", "English"], i, ifc, rule, im, result = {}, recognizedList}, i = Binarize@ColorNegate@image; ifc = ImageForestingComponents[ColorNegate@i, Automatic, {First@ImageDimensions@i, 1}]; rule = {h__, {0 ..}, w : {0 ..}, {0 ..}, t__} -> {h, w, w, t}; im[x_] := Transpose[ Transpose[Replace[ifc, Except[x] -> 0, {2}] //. rule] //. rule]; tr = TextRecognize[Image[im[#]], Language -> "Spanish"] & /@ Range[2, Max@ifc]; recognizedList = Flatten@List[tr]; gt = args[ServiceObject]; translatedText = gt["Translate", "To" -> to, "Text" -> #] & /@ recognizedList; translatedText ]

Connect to the Microsoft Translator API.

In[2]:=
Click for copyable input
mt = ServiceConnect["MicrosoftTranslator"];

Import an image.

In[3]:=
Click for copyable input

Select the area where the text appears.

In[4]:=
Click for copyable input
col = ImageTake[img, {970, 1920}, {770, 1300}]
Out[4]=

Use the previously defined function to get the translation.

In[5]:=
Click for copyable input
translation = ImageTranslate[col, ServiceObject -> mt]
Out[5]=

You can even convert the text to real colors with the Wolfram Language!

In[6]:=
Click for copyable input
Interpreter["Color"] /@ translation
Out[6]=

Related Examples

de es fr ja ko pt-br ru zh