I am trying to add OCR to PDFs and am using pdfsandwich to do so. The problem is that pdfsandwich processes the image when doing OCR which changes what the document looks like.

Is there any way to ensure that the PDF image remains completely unchanged? if this is not possible with pdfsandwich, accomplishing this with an alternate application would be acceptable.

Example Before: Before

Example After: After

You can easily see the drop in quality after running the pdf through pdfsandwich.

I've looked at the pdfsandwich documentation and was unable to find anything about leaving the image unchanged.


I contacted Tobial Elze, the creator of pdfsandwich, with my question. Here is their response:

I'm fully aware of what you mean and I agree that this would be a useful feature. It is currently not possible to fully keep the original image, because pdfsandwich calls Tesseract to create the final pdf, which internally performs some processing which is outside the scope and control of pdfsandwich.

By default, pdfsandwich converts input images to black-and-white, as you can see in your own example. You can get closer to the original image by enforcing it to use color (for color images) by option -rgb or, as in your case, gray scale by option -gray. You may want to try the latter on your example to see if it improves.

Apart from that, if you find that pdfsandwich's "improvement" functions prior to OCR do not improve the image but rather make it worse instead, you can switch off all preprocessing by option -nopreproc.

I hope that helps somewhat?

Because my PDFs are already gray-scale, simply adding the flag -gray seems to preserve the original quality. -rgb is the equivalent flag for colored images.

| improve this answer | |

I had the same desire because pdfsandwich badly recompressed tiny PDF images, ballooning the PDF filesize. Here's a complicated way I handled it with a one page PDF that had already been cleaned up for the benefit of OCRing:

  1. Make backups of the original PDF. If you mess things up, you'll have the backup.
  2. Run pdfimages -list original.pdf
  3. Use a calculator to multiply together the width and the height of the image. Let's say the image is 192 × 643. Multiplying that, we get 123456. We use that in the next step.
  4. Run pdfsandwich -nopreproc -maxpixels 123456 original.pdf

Now we have an OCRed PDF with images we want to replace. This part is a bit trickier.

  1. Run pdfimages -list original.pdf
  2. Write down the object number in the original PDF. Let's say that's 123. We use that below.
  3. Run pdfimages -list original_ocr.pdf
  4. Write down the object number in the OCRed PDF. Let's say that's 456. We use that below.
  5. Open both the original and OCRed PDF in a text editor that can properly handle copying and pasting binary blobs.
  6. Find the line 123 0 obj in the original PDF. Copy the line below that together with all following lines until the next endobj line.
  7. Find the line 456 0 obj in the OCRed PDF. Erase the line below that together with all following lines until the next endobj line. Replace the lines you removed by pasting what you copied from the original PDF.
  8. Save the OCRed PDF as a new filename.
  9. Open the new filename from the previous step in your PDF viewing program to check that you can see the page, that the OCRed text is there, etc.
  10. Close the text editor without saving.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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