TIFF OCR: a multi-page TIFF to text, PDF and Word
1,878 of 1,955
words found, all 10 TIFF pages
One command reads every page
10 of 10 pages in one text file
34 s for 10 pages, 2 threads, shared Mac
On a Mac, in a folder with scan.tif:
brew install tesseract
tesseract scan.tif scan
That writes scan.txt, with a form feed between pages. Apart from those, the text matched Tesseract on the 10 source JPEGs at --dpi 300, byte for byte.
A searchable PDF in the same pass
tesseract scan.tif scan txt pdf
That one run writes scan.txt and scan.pdf. Leave out txt to get only the PDF.
6.6 MB PDF, from a 48.8 MB TIFF
1,875 words in it, read by pdftotext
It shrinks because Tesseract stores each page as a JPEG inside the PDF.
OCRmyPDF needs a DPI in the TIFF
OCRmyPDF 17.13.0 takes a multi-page TIFF as it is:
ocrmypdf --jobs 2 scan.tif scan.pdf
It made a 10-page PDF/A, and pdftotext found 1,853 words. The PDF is 38.0 MB: OCRmyPDF did not turn the pages into JPEGs.
With no resolution tag in the TIFF, it stops:
DpiError: Input file is an image, but the resolution (DPI) is not credible. Estimate the resolution at which the image was scanned and specify it using --image-dpi.
Give it a DPI, and you get the same text as the 300 dpi TIFF:
ocrmypdf --jobs 2 --image-dpi 300 scan.tif scan.pdf
pytesseract with Image.open reads page 1
This reads page 1 and stops. The other 9 pages go quietly: no error, no warning.
import pytesseract
from PIL import Image
print(pytesseract.image_to_string(Image.open('scan.tif')))
pytesseract saves the image for Tesseract with Pillow, and Pillow saves only the current frame. Pass the path, or loop over the frames with tiff.py:
import sys
import pytesseract
from PIL import Image, ImageSequence
with Image.open(sys.argv[1]) as tif:
for page in ImageSequence.Iterator(tif):
print(pytesseract.image_to_string(page))
| Call | Pages | Words |
|---|---|---|
| Image.open('scan.tif') | 1 | 197 |
| the path, 'scan.tif' | 10 | 1,878 |
| python tiff.py scan.tif | 10 | 1,878 |
The Group 4 TIFF lost the most words
| TIFF | Size | Words |
|---|---|---|
| LZW, color, 300 dpi | 48.8 MB | 1,878 |
| JPEG inside, quality 92 | 14.5 MB | 1,883 |
| Group 4, black and white | 409 KB | 1,796 |
| LZW, no DPI tag | 48.8 MB | 1,877 |
Group 4 keeps one bit a pixel. I made it with a 50% threshold, my own pick.
With no DPI tag, Tesseract guessed 625 to 711 dpi and still read every page.
TIFF to Word: the text, not the layout
On a Mac, textutil turns the text file into a Word file:
textutil -convert docx scan.txt
Each form feed became a page break, and textutil read back the same text, byte for byte. You get the words, not the page layout or the pictures.
pandoc scan.txt -o scan.docx reads the file as Markdown.
It joins lines into paragraphs, so a word split at a line end stays in
two pieces, like some- thing: 1,868 words found, not
1,878. It also curls straight apostrophes.
How I tested it
- Pages: the 10 scans of OCR in n8n, proofread Wikisource book pages, 1,955 true words, as 1920 px color JPEGs with no DPI.
- ImageMagick 7.1.2 joined the JPEGs in page order into 4 TIFFs. The two LZW TIFFs have the same pixels as the JPEGs.
- Tesseract 5.5.4, OCRmyPDF 17.13.0 with --jobs 2, and pytesseract 0.3.13, on an Apple Silicon Mac.
- One run at a time, lowest priority, 2 Tesseract threads. Other jobs shared the Mac, so times are rough.
- Score: true words found in order. A page counts as read when half its words are found. PDF text comes from pdftotext.
magick *.jpg -compress LZW -units PixelsPerInch -density 300 scan-lzw.tif
magick *.jpg -compress JPEG -units PixelsPerInch -density 300 scan-jpeg.tif
magick *.jpg -colorspace Gray -threshold 50% -type bilevel -define tiff:photometric=min-is-white -compress Group4 -units PixelsPerInch -density 300 scan-g4.tif
magick *.jpg -compress LZW scan-nodpi.tif
What I did not test
- Windows and Linux, and OCR inside Word itself.
- Other Group 4 thresholds, and scans in other languages.
- TIFFs straight from a scanner: I made mine from JPEGs.
TIFF to text, no install
Run it yourself. Our Apify Actor reads PDF, PNG, JPEG, WEBP and TIFF, each TIFF frame as a page, for $0.006 a page. Its code found 1,879 of these 1,955 words in the TIFF.
Or send us the scan. Tack, our AI, does the job for $29, up to 50 pages. First page free.
Questions people ask
Can Tesseract OCR a multi-page TIFF?
Yes. tesseract scan.tif scan read all 10 pages of my TIFF into one text file, with a form feed between pages: 1,878 of 1,955 words.
Why does pytesseract read only the first page of a TIFF?
Image.open starts on the first frame, and pytesseract saves only that frame for Tesseract. Pass the file path, or loop with ImageSequence.Iterator: both read all 10 pages.
How do I make a TIFF a searchable PDF?
Run tesseract scan.tif scan pdf. OCRmyPDF works too: ocrmypdf --jobs 2 scan.tif scan.pdf. If the TIFF has no DPI tag, add --image-dpi and the scan's DPI (I used 300).