DjVu OCR: check the text layer first
96%
of words were already in the files
The text was already there
Both of our DjVu books came with a hidden text layer. Print it first:
djvutxt book.djvu | head
| Text | Words |
|---|---|
| The layer in the files | 1,877 |
| Fresh OCR, ocrodjvu --render=all | 1,881 |
Fresh OCR found 4 more words. If the old layer reads well, keep it and skip the rest of this page.
OCR inside the DjVu: ocrodjvu
ocrodjvu runs Tesseract on each page and writes the text into the DjVu file.
brew install djvulibre tesseract
python3.13 -m venv venv
venv/bin/pip install djvulibre-python ocrodjvu
cp book.djvu book-ocr.djvu
venv/bin/ocrodjvu --render=all -j 2 -l eng --in-place book-ocr.djvu
| Options | Words | Time |
|---|---|---|
| --render=all -j 2 | 1,881 | 53 s |
| --render=all | 1,881 | 90 s |
| The default (mask) | 958 | 16 s |
- Add --render=all. The default reads only the black and white layer. On our second book's 5 pages it is empty: no text, and exit code 0.
- Use --in-place on a copy. With -o, the save step failed and left a 0 byte file.
- Old text is replaced, with or without --clear-text.
DjVu to PDF drops the text
OCRmyPDF does not read DjVu: it stopped with UnsupportedImageFormatError. Convert first with ddjvu, which comes with DjVuLibre.
ddjvu -format=pdf book.djvu book.pdf
ocrmypdf -l eng --jobs 2 --optimize 3 book.pdf book-ocr.pdf
| Steps | Words | |
|---|---|---|
| ddjvu only | 0 | 133 MB |
| Then ocrmypdf | 1,880 | 133 MB |
| Then ocrmypdf --optimize 3 | 1,880 | 21 MB |
| ddjvu -quality=80, then ocrmypdf | 1,414 | 11 MB |
| ddjvu -mode=black, then ocrmypdf | 957 | 0.2 MB |
ddjvu writes each page as a lossless picture and leaves the text layer out. -quality=80 turned the bottom half of our 5 large pages flat gray, and -mode=black left them blank.
For text only, make a TIFF
ddjvu's TIFF output kept every page whole at -quality=80: 11.5 MB for our 10 pages.
OCRmyPDF read it too, 1,879 words, but wrote a 77 MB PDF.
ddjvu -format=tiff -quality=80 book.djvu book.tif
The 133 MB PDF is too big for our Actor: it takes 100 MB a file and 14 MB a page. The TIFF fits.
Same 10 pages, one scorer
The pages come from two public domain DjVu books on Wikimedia Commons, proofread on Wikisource: 1,955 words. The score is true words found in order.
How we ran it
- DjVuLibre 3.5.30 from Homebrew (its tools print 3.5.29), Tesseract 5.5.4.
- ocrodjvu 0.14 and djvulibre-python 0.9.3, on Python 3.13.
- OCRmyPDF 17.14.1, in the same venv.
- We cut the 10 pages out of the two books with djvused and bundled them with djvm.
- Five pages are 500 dpi with a black and white layer.
- Five are 300 dpi pictures, 4,550 to 4,613 pixels wide, with an empty black and white layer.
- For the ocrodjvu runs in the table we removed the text layer first (djvused remove-txt). The replace test used the file with its layer.
- The -o failure: the save wrote a 0 byte file, then djvused exited with status 10. The library save behind it failed on every multi-page file we tried, the untouched Commons books too, and worked on the one single page we tried.
- The gray pages: djpeg reported "Corrupt JPEG data" on page 6, the one we checked. All 5 decoded flat gray in the lower half.
- Times: one run at a time, with --jobs 2 for OCRmyPDF, at low priority, while other jobs ran.
- OCRmyPDF took 95 s, and 224 s with --optimize 3.
- Licenses: DjVuLibre GPL-2.0 or later, ocrodjvu and djvulibre-python GPL-2.0, Tesseract Apache-2.0, OCRmyPDF MPL-2.0. We run them and ship none of them.
Scanned pages to text, no install
Run it yourself. Our Apify Actor reads PDF, PNG, JPEG, WEBP and TIFF, not DjVu, for $0.006 a scanned page. Its code found 1,876 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 OCRmyPDF read a DjVu file?
No. OCRmyPDF 17.14.1 stopped with UnsupportedImageFormatError and exit code 2. Convert the file to PDF with ddjvu first.
Does ddjvu keep the text layer in the PDF?
No. Our PDF from a DjVu file with text had no fonts and no text. Run OCR on the PDF after.
Why did ocrodjvu give me empty pages?
Its default, --render=mask, reads only the black and white layer. A page where that layer is empty gets no text. Add --render=all.
Does ocrodjvu run on Python 3?
Yes. ocrodjvu 0.14 on PyPI is a Python 3 port. We ran it on Python 3.13.
How do I get plain text out of a DjVu file?
Run djvutxt book.djvu > book.txt. It prints the hidden text layer. If it prints nothing, run ocrodjvu first.