I have lots of high quality, high resolution black and white raster images of scanned text with a minimal amount of noise, around 6 Gigapixels or 500 pages per document. They use too much disk space for my liking. Is there a tool which can compress raster images using ocr, losslessly?

It would work like this:

  1. Do word-by-word ocr on input file.
  2. Create a font that matches the one in the scanned file.
  3. Put the ocr'd text into a pdf together with this font, creating an image that contains just the ocr'd parts of the original (and is tiny because it's just a font+plain text).
  4. Do a raster difference of the ocr'd part and the original, then add just the difference to the ocr'd image as a raster overlay.

Because the difference should be small (just some noise and the occasional word or glyph that couldn't be recognized), this should achieve very high compression ratios.

  • 1
    Wouldn't something to turn them into a vector format (like SVG) be more suitable?
    – Izzy
    Commented May 13, 2020 at 20:43
  • @ThomasWeller Compression algorithms usually aren't smart enough to do that, not to such a degree. OCR is essentially a compression algorithm optimized for images of text.
    – Nobody
    Commented May 13, 2020 at 21:42
  • @Izzy The scans are good (600dpi), but the usual vecorization tools (well, I just know Inkscape) would still mostly just degrade quality (certainly not lessless) and blow up file size (they would not be able to deduplicate characters, which is the point - a single character in a vector format takes more space, you only start saving by reusing the same vectorization lots of times). What I describe is essentially vectorization, but in a specialized way so it's actually benecificial.
    – Nobody
    Commented May 13, 2020 at 21:45
  • @ThomasWeller I explicitly state that the images are black and white (not grayscale). I don't think you thought about this a lot, are you aware 500 pages at 600 dpi are around 6 Gigapixels and hundreds of MB, losslessly compressed on a per page basis? And I'm supposed to put that into a single standard image file and pray to the compression gods? The only way this is going to shrink significantly is using ocr. By using ocr (and possibly some further despeckling on the intermediary difference image) this could shrink to tens of MB. No ordinary compression will reach such a ratio.
    – Nobody
    Commented May 13, 2020 at 22:08
  • (the despeckling wouldn't be lossless anymore, yes, but even without it, the ratio should be good. And a despeckling step at this precise point would be ok and much less destructive than turning up lossy compression in a standard compression algorithm)
    – Nobody
    Commented May 13, 2020 at 22:10

1 Answer 1


Such a program probably doesn't exist. But the question is based on the wrong assumption that you need formal ocr to get the desired compression ratios. There is a actually at least one image compression algorithm, jbig2, that uses similar symbol classification techniques without actual ocr.

This algorithm basically creates its own "font" (symbol table), does ocr using that font and compresses the image like that.

If ocr is not actually needed, this is great, but if you would like the text ocr'd anyway, then filesize will be about twice what it could be because the text is stored once as formal, proper text outputted by the ocr algorithm and once in the implicit form generated by jbig2.

Note: Use jbig2 only in lossless mode. In lossful mode jbig2 can make mistakes such as replacing a 1 with an l or other mistakes that you would expect an ocr algorithm to make.

Note 2: I won't accept this answer, I'm hoping the asked for program still turns up.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.