Skip to content

file size increase for pdf/a #293

Closed
Closed

Description

@femifrak

OCRmyPDF is really marvelous! Thanks!

I have one question regarding output file size: Unless explicitly selecting pdf as output type, I have quite large file sizes (~4x) after "ocrmypdf in.pdf out.pdf". The pages are scanned text, i.e. actually there are no gray pixels only black or white ones. Only "--output-type pdf" keeps the file size similar.

For the first page (the others are similar) "pdfimages -list in.pdf" gives:

page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    2697  4533  icc     1   1  ccitt  no        17  0   600   601 88.3K 5.9%

out.pdf results in:

page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    2697  4533  rgb     3   8  image  no        12  0   600   601  385K 1.1%

Even --optimize 3 results in double file size for out.pdf (saved as pdf/a):

page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    2697  4533  gray    1   1  image  no        34  0   600   601  203K  14%

Is a conversion obligatory for pdf/a? Or is there a way to keep the original image type AND generate pdf/a?

Activity

  1. jbarlow83 commented on Sep 13, 2018

    @jbarlow83
    Collaborator

    Input file has a 1bpp image connected to an ICC profile (color: icc). That's what makes this case more complicated. Since there's an ICC profile, it could describe any two colors in any color space (it could call for pink and blue in CMYK).

    Now I have to rasterize the PDF to OCR, but with --output-type pdf (unless the "image processing" settings are on), the raster image never makes it to the final PDF.

    For PDF/A, I send the OCR+PDF to Ghostscript. It probably decides for its own reasons to convert the unusual 1bpp with colorspace to 24bpp RGB. Maybe it has to do this for PDF/A but I don't know actually know for sure. I optimize whatever output it gives me.

    With --optimize 3 the optimizer is able to determine that the page can be reduced to back to 1 bpp (this relevant strategy is disabled under default optimization settings). This hits another rare special case: quantizing an image all the way to 1 bpp somewhat rare, so I don't test for opportunities to change compression after quantizing. That is something I can add eventually.

  2. femifrak commented on Sep 13, 2018

    @femifrak
    Author

    Thank you for the explanation. But one thing I don't understand:

    with -output-type pdf (unless the "Image processing" settings are turned on), the raster image will never make it into the final PDF.

    Did you mean "without" instead of "with"? With "-output-type pdf" I get small outfile sizes. Therefore, I think this option makes the original PDF images the final PDF.

    I would think that most of the scanned PDFs contain old documents, many of which are only b/w. That is, I don't think b/w is such a rare special case, don't you? What do you think of an option to convert gray images to b/w for output? Gray images would be better for tesseract (?) and b/w output would be better for reading and for file size. This would be the same reasoning as for --clean-final, but --clean-final doesn't convert to monochrome. (Apart from the programming effort, of course ...).

    change compression after quantizing. That is something I can add eventually.

    very good idea :)

  3. jbarlow83 commented on Sep 13, 2018

    @jbarlow83
    Collaborator

    What I mean is that with --output-type pdf, OCR is grafted on the original PDF without modifying the original images (other than optimizing it in place). With --output-type pdfa, I give the PDF to Ghostscript and it "prints" a new PDF that looks like the original (changing anything it wants to).

    The special case is a PDF that contains an image that the image quantizer is able to reduce to 1 bpp. Images that are already 1 bpp are quite common and get optimized.

  4. femifrak commented on Sep 14, 2018

    @femifrak
    Author

    ah, okay,
    I am not familiar with github. Should I close this issue and place my wish (convert gray images to monochrome) into pull request? (am not sure what a pull request is.)
    Or is this something the owner does?

  5. jbarlow83 commented on Sep 14, 2018

    @jbarlow83
    Collaborator

    It depends on the github project and what the owner wants to do. In this case I'll close it and make a new issue.

    Pull requests are for when you want to contribute a change to the project.

  6. femifrak commented on Sep 15, 2018

    @femifrak
    Author

    I don't know whether the new issue covers what I meant.
    I did not only mean optimization of a pdf with images that only use b/w (and a bpp>1), but a real transformation of color or grayscale images to b/w.
    Abbyy for instance recommends scanning text in grayscale (for better ocr) and offers to save as b/w.

  7. jbarlow83 commented on Sep 15, 2018

    @jbarlow83
    Collaborator

    Currently pngquant attempts to quantize color and grayscale images and will sometimes get them to 1bpp. The file size increase occurs because the optimizer doesn't change the compression of 1bpp images after quantizing them.

    I don't intend to offer save as b/w because some workflows (mine for example) is mostly b/w with a few color images per file, and I'd rather not introduce an option that effectively ruins the output if you use it on the wrong file.

  8. femifrak commented on Sep 15, 2018

    @femifrak
    Author

    I thought of an additional parameter similar to --clean-final which could also ruin the outfile. If this happens it would be the responsibility of the user. If it improves recognition quality of tesseract ...

  9. aspik commented on Feb 6, 2020

    @aspik

    I know this might be not the right place, but I didn't want to create a new "issue".

    I don't intend to offer save as b/w because some workflows (mine for example) is mostly b/w with a few color images per file, and I'd rather not introduce an option that effectively ruins the output if you use it on the wrong file.

    Could you please tell a bit more about your workflow? I have documents which contains just black text with a small color graphic (corporate logo). Additionally one site has signatures which are made with a blue pencil. What would be the best way to scan and process this kind of documents (contracts)? I don't want to lose the color information.

    Currently I'm scanning it as color text with 600dpi (using VueScan). After passing it through ocrmypdf the size did not reduced much (~700KB). When using the jbig2-lossy compression 3 the size was halved (14MB -> 7MB for an 8 pages document). I'm perfectly fine with the size in case of storing it locally.

    However, sometimes I want to send this kind of document per e-mail and in this case even the 7MB is not optimal. I would like to convert it to b/w as it does not matter if the corporate logo is red-blue or just black. How would that be harmful if there would be a save/convert as b/w option?

    Maybe I'm missing something here and there is a different way to reduce the file size?

    Thanks for the excellent app!

  10. ferdiga commented on Aug 2, 2024

    @ferdiga

    Hi
    in my case the creation of a PdfA increased the size by a multiple of 500 !!!
    IMO I identified the culprit: gs can not handle mixed portrait and landscape well.
    After separating portrait and landscape files in 2 separate files, ocrmypdf performed extremely well and reduced the file size of each file.

    A solution could be to split each file into single page files, run ocrmypdf (and hence gs) on each and put these together again?

    probably if ocrmypdf could do it (on demand or after detecting portrait/landscape automatically) it would solve the problem unless gs can handle this.
    Probably this might have other unintended results?

  11. jbarlow83 commented on Aug 2, 2024

    @jbarlow83
    Collaborator

    @ferdiga Please open a new issue with an example file if you want support for that.

  12. locked as resolved and limited conversation to collaborators on Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions