Sub topic 7
Selecting Languages and Trained Data
This chapter assumes you know basic Python, can install packages with pip, understand images as pixel arrays, and can run commands in a terminal. The examples use Tesseract through pytesseract.
Tesseract does not recognize text from pixels by using one universal model. It loads trained data for one or more languages. Trained data contains the language and character information that Tesseract uses during recognition. Choosing the correct data can improve both accuracy and the way words are separated.
Language Codes and Trained Data
A Tesseract language is identified by a short code. Common examples include:
| Language | Tesseract code | Typical use |
|---|---|---|
| English | `eng` | English documents |
| German | `deu` | German documents |
| French | `fra` | French documents |
| Spanish | `spa` | Spanish documents |
| Japanese | `jpn` | Japanese text |
The language code is not always the same as a system locale. For example, Brazilian Portuguese commonly uses por, just like other Portuguese data. Check the trained-data files installed on your system instead of guessing a code.
List the languages available to your Tesseract installation:
tesseract --list-langsThe output usually includes eng, and may include additional languages such as deu or fra. If a requested language is missing, install its trained-data package for your operating system. The package is separate from the Python package:
python -m pip install pytesseract pillowThis installs the Python interface and Pillow, but it does not install the Tesseract executable or its language files.
Selecting One or More Languages
Pass a language code with the lang argument. The following example asks Tesseract to use English:
from PIL import Image
import pytesseract
image = Image.open("scan.png")
text = pytesseract.image_to_string(image, lang="eng")
print(text)For a document that contains both English and German, combine the languages with a plus sign:
from PIL import Image
import pytesseract
image = Image.open("bilingual-page.png")
text = pytesseract.image_to_string(image, lang="eng+deu")
print(text)Tesseract then has access to the character patterns and word information from both language models. Every language in the combination must be installed. If deu is missing, the call fails instead of silently downloading it.
Use the smallest language set that matches the image. Adding many languages can slow recognition and can make ambiguous characters harder to classify. For example, a word containing O, 0, and accented characters may receive different interpretations when several models are active.
Language Versus Script
A language describes words and spelling. A script describes the writing system, such as Latin, Cyrillic, or Arabic. Several languages can share one script, and one language can appear in more than one script.
If you need to identify the script or orientation before recognition, Tesseract provides an orientation and script model:
tesseract page.png stdout --psm 0 -l osdThe osd model is useful for deciding whether a page is rotated and for detecting broad script information. It is not a replacement for a language model. For final extraction, use the actual language data when you know the document language.
Supplying a Custom Data Directory
Tesseract searches standard directories for files such as eng.traineddata. If your files are stored elsewhere, provide the directory through --tessdata-dir:
from PIL import Image
import pytesseract
image = Image.open("scan.png")
config = r'--tessdata-dir "/opt/tesseract/tessdata" --psm 6'
text = pytesseract.image_to_string(image, lang="eng", config=config)
print(text)The directory must contain the requested trained-data file. On Windows, use a raw Python string or escape backslashes in the path.
Testing Language Choices
Use representative images rather than a single easy page. Include accents, numbers, punctuation, and words that differ between languages. Record the language setting and compare the output with known text. A confidence score can help identify uncertain regions, but it does not prove that the language choice is correct.
If a document has separate regions, process them independently when possible. For example, recognize an English body paragraph with eng and a Japanese caption with jpn. Region-specific language selection often works better than using eng+jpn on the whole image.
Exercise
A page contains English paragraphs and French headings. What language setting should you test first, and when might you process regions separately?
Show answer
Test eng+fra if the layout mixes both languages throughout the page. Process regions separately with eng and fra when the headings and paragraphs can be reliably cropped, because each region then uses a narrower and more specific model.