Marcio Cunha

How to extract text from images in the terminal with Tesseract OCR

Learn how to install and use Tesseract OCR to extract text from screenshots and scanned documents directly from the command line.

Marcio Cunha•5 min
Also available in:PortuguêsEspañol
Summary
  • Tesseract OCR converts images containing printed text into editable text files using optical character recognition.
  • Installation on Debian or Ubuntu based Linux systems requires only the standard apt package manager.
  • Basic command-line usage requires specifying the source image and the desired output text file.
  • Support for multiple languages depends on the additional installation of specific training data packages.
  • Automating repetitive OCR tasks can be easily integrated into shell scripts for maximum productivity.

What is Tesseract OCR and why use it in the terminal

Working with scanned documents, screenshots, or receipts often puts us in front of an annoying problem: the image contains valuable information, but the text is completely locked inside the pixels. This is precisely where OCR comes in, an acronym for Optical Character Recognition. In practice, this technology analyzes the visual shapes of letters and numbers in an image and converts them into plain text that you can edit, copy, and search. Among the available tools, Tesseract stands out as an open-source project maintained by Google, offering high accuracy without relying on paid cloud services.

For those who handle automation and prefer the agility of the keyboard, using Tesseract through the command line is a true game-changer. Instead of opening heavy graphical editing software or dubious web portals every time you need to read a screenshot, you execute a single instruction in the terminal. This workflow integrates seamlessly into larger routines, allowing you to process hundreds of files in seconds. If you have already automated tasks using tools like converting scanned PDF files, adding Tesseract to your daily arsenal drastically expands your document manipulation capabilities.

Installing Tesseract OCR and language packages

Before we begin character extraction, we need to ensure the utility is installed on your operating system. On Debian and Ubuntu based systems, such as Linux Mint or Pop!_OS, the process is extremely straightforward and uses the distribution's official package manager. In addition to the main program, it is essential to install training data packages for the languages you intend to recognize, ensuring the program understands accents and local specificities.

Open your terminal and run the commands below to perform a complete installation of the tool and the English and Portuguese dictionaries:

sudo apt update
sudo apt install tesseract-ocr tesseract-ocr-eng -y

With this instruction, the system downloads both the processing engine and the linguistic reference files. To verify that everything went correctly and confirm the installed version, simply type the verification command in the terminal:

tesseract --version

How to perform basic text extraction from images

With the environment properly prepared, using Tesseract day-to-day is surprisingly direct. In its most fundamental syntax, the command takes two main arguments: the image file you want to analyze and the base name of the text file that will be generated as output. The program automatically appends the .txt extension to the final file, saving time and keeping your working directory organized.

Imagine you have a screenshot named receipt.png in your current folder and want to extract all its textual content into a file named result. To perform this operation, run the instruction below in your terminal:

tesseract receipt.png result

After a few moments of processing, the result.txt file will be ready in the same folder. You can open it with any text editor or view it directly in the terminal using the cat result.txt command to check the accuracy of the reading performed by the OCR engine.

Adjusting language and improving reading accuracy

By default, Tesseract assumes that the text contained in the image is written in English. If your image contains words in other languages, technical terms with accents, or special characters, the extraction result may present flaws and interpretation errors. To fix this, we explicitly inform the program which language it should use during the visual scan through the -l configuration parameter.

To indicate that the document is in English or another specific language, the command structure gets a small addition right after the source image name. See how to structure this call in the terminal:

tesseract document.jpg output -l eng

This simple adjustment directs the engine to prioritize the dictionary and grammatical rules of the selected language, drastically raising the success rate. If your document mixes terms from different languages, you can combine dictionaries by separating them with a plus sign, such as -l eng+por, ensuring no foreign word escapes the scan.

Automating workflows with shell scripts

The true power of using Tesseract in the terminal emerges when we combine the tool with small automation scripts. If you receive dozens of scanned invoices, contracts, or screenshots daily, performing the process manually on each file becomes unfeasible. We can write a simple loop in Bash to scan an entire directory and convert all PNG images automatically.

The code snippet below loops through all image files in the current directory, extracts the text from each of them, and saves the result keeping the original file name:

for img in *.png; do
  base_name="$(basename "$img" .png)"
  tesseract "$img" "$base_name" -l eng
  echo "Processed: $img"
done

This approach eliminates repetitive work and integrates text extraction into your productivity ecosystem, working similarly to other command-line utilities that make daily engineering work easier, such as audio conversion in the terminal or handling structured data. To explore these architectural concepts further in production, see our related technical analysis on How to test 18650 lithium batteries with multimeter and load.

Final considerations on efficient OCR use in the terminal

Tesseract OCR proves that we do not need to resort to complex software or paid cloud solutions to solve everyday text extraction tasks. Mastering this tool right in the command line saves precious time and guarantees total privacy for your documents, since all processing happens locally on your machine. Whether to digitize old notes, automate invoice reading, or integrate visual recognition into data pipelines, Tesseract offers unmatched robustness and flexibility for Linux and Unix-like system users.