Tesseract 生成可搜索的 PDF,深度从 8 位回到 1 位 (tess4j)
我有一个 1 位颜色深度的 PDF 作为 OCR 处理的输入(tess4j,5.0.0),大约为。每个 30kb。处理后,每个PDF有120-130kb,并以8位颜色深度保存,这可能是文件大小增加的主要原因。
我想知道是否有一种方法可以在 Tesseract 或相关库中设置颜色深度,或者有另一种方法可以处理这个问题。
ITesseract instance = new Tesseract();
instance.setDatapath("/path/to/tessdata");
instance.setPageSegMode(ITessAPI.TessPageSegMode.PSM_SINGLE_COLUMN);
List<ITesseract.RenderedFormat> formats = new ArrayList<(Arrays.asList(ITesseract.RenderedFormat.PDF));
instance.createDocumentsWithResults(inputPdf.getPath(), "/path/to/result", formats, ITessAPI.TessPageIteratorLevel.RIL_WORD);
非常感谢任何帮助。
I have a PDFs with 1-bit color depth as an input for OCR processing (tess4j, 5.0.0) with approx. 30kb each. After processing, each PDF has 120-130kb and is saved with 8-bit color depth, which is probably main cause of file size increase.
I would like to know if there is a way to set color depth within Tesseract or associated libs or there is another way to handle this.
ITesseract instance = new Tesseract();
instance.setDatapath("/path/to/tessdata");
instance.setPageSegMode(ITessAPI.TessPageSegMode.PSM_SINGLE_COLUMN);
List<ITesseract.RenderedFormat> formats = new ArrayList<(Arrays.asList(ITesseract.RenderedFormat.PDF));
instance.createDocumentsWithResults(inputPdf.getPath(), "/path/to/result", formats, ITessAPI.TessPageIteratorLevel.RIL_WORD);
Any help greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终,我想出了一个解决方法 - 您可以通过定义 RendererFormat 来指定输出。我将其从 PDF 更改为 PDF_TEXTONLY,生成了一个 pdf (~7kb),文本位于正确的位置,但没有原始扫描/图像。
然后我使用 PDFBox 功能从原始 pdf 中提取图像。可以指定 DPI,这也有助于减小文件大小。
然后只需将图像添加到纯文本 pdf 中作为水印 (如何使用 iText 在文本下方插入图像作为 pdf 背景?)。这有助于在 300 DPI 下将大小从 120-130 kb 减少到 60 kb(在较低 DPI 下甚至更小),考虑到这是一个 OCR 处理的 pdf,原始大小为 30kb,这非常好。我知道这不是最好的解决方案,我很乐意提供任何其他贡献或答案。
Eventually, I came up with a workaround - you can specify the output by defining RendererFormat. I changed that from PDF to PDF_TEXTONLY, which produced a pdf (~7kb) with the text in the right position but without the original scan/image.
Then I used PDFBox functionality to extract image/images from original pdf. It is possible to specify DPI which also helps to reduce the file size.
Then just add an image to the text-only pdf as a watermark (How to insert a image under the text as a pdf background using iText?). This helped reduce the size from 120-130 kb to 60 kb with 300 DPI (even less with lower DPI), which is great given that it is an OCR processed pdf with an original size of 30kb. I know this is not the best solution and I'll be happy for any other contribution or answer.