Optical Character Recognition (OCR) allows you to extract readable text from a static image file. While many people use web-based tools or graphical applications for this, you can extract text incredibly fast directly from the Linux terminal using the open-source Tesseract OCR engine.
How to Install Tesseract
Tesseract is one of the most accurate open-source OCR engines available. It is widely supported and can be installed via your distribution’s package manager.
- On Ubuntu/Debian systems:
sudo apt install tesseract-ocr - On RHEL/Fedora systems:
sudo dnf install tesseract - On Arch Linux:
sudo pacman -S tesseract
By default, Tesseract installs support for the English language. If you need to extract text in another language (e.g., French), you will need to install the specific language pack (e.g., sudo apt install tesseract-ocr-fra).
How to Extract Text from an Image
Once installed, using Tesseract is straightforward. You only need to provide the input image file and the desired name for the output text file.
- Open your terminal.
- Navigate to the directory containing your image using the
cdcommand. - Execute the following command syntax:
tesseract input_image.png output_text
For example, if you have a scanned document named invoice.jpg and you want to extract the text into a file named data.txt, you would run:
tesseract invoice.jpg data
(Note: You do not need to add the .txt extension to the output filename; Tesseract adds it automatically).
How to Print the Output Directly to the Terminal
If you only have a quick screenshot and do not want to generate a permanent text file on your hard drive, you can tell Tesseract to output the extracted text directly into your terminal window.
To do this, use the stdout command as the output destination:
tesseract screenshot.png stdout
The extracted text will immediately appear on your screen, allowing you to copy it directly from your terminal session.