An image is scanned in using SANE with a 300dpi spec:

$ scanimage --format=pnm --mode='black & white' --resolution=300 >sample.pbm

Proving that it produced 300 dpi is a pain in the ass because (apparently) PBM files do not have page dimensions in the metadata, only the total count of pixels. So identify -verbose sample.pbm | grep -i resolution yields nothing. I must do the math myself:

$ identify sample.pbm
sample.pbm PBM 2464x3437 2464x3437+0+0 1-bit Bilevel Gray 1.00959MiB 0.050u 0:00.050

(side note: annoying that it says “Bilevel Gray” b/c it’s a contradiction)

Anyway, width is 2464 dots. It was an A4 page, so that’s 210mm wide IIRC. 210mm=8.27″. So 2464 ÷ 8.27 → 297.94… Close enough. That’s 300 dpi. There is a bit of loss because scanners have a dead area near the edges just like printers do.

Import into GIMP, alt+enter for image properties. “Resolution: 72×72 ppi”. A lie! And right before that line, we have “print size: 34.22 × 47.74 inches”. Another lie!

To be fair, we can set the print size in GIMP (Image → Print Size…). But it’s not good that it makes assumptions and misleads users. In the very least there should be a ⚠ next to assumed numbers, and a mouse-over explainer.

If you correctly set the print size to get the correct DPI, then export as a different PBM, the print size info is lost. This may be a limitation of the PNM format.

Scribus is worse. You simply load a 300 dpi pbm file and it throws a warning in your face saying 72×72dpi is poor quality. Which is true only if it knows the dpi or page size.

Unrelated followup question: what procedure gives the smallest lossless PDF (from PBM)?

My current method needs improvement. This is what I do now:

$ unpaper -t pbm from_scanner.pbm deskewed.pbm
$ optipng --strip all -o7 -out intermediate.png deskewed.pbm; # better compression than ImageMagick
$ pdfjam --a4paper -o final.pdf intermediate.png; # contains png files as-is with no transcoding

Results are decent but could be better if TIFF is the intermediate format, not PNG. I think pamtotiff could have potential. But it’s annoying that PBM files are converted to grayscale, which seems unwise. It’s not well documented. The man page muddles through the “resolution” section with no examples. And a web search yields only copies of the man page and a couple overly basic examples which do not set the resolution.

The reason I think TIFF would be a better intermediate format is because I can use cbj2 to produce the tiniest possible bilevel image, and when that is converted to PDF (ddjvu), the resulting PDF is still smaller than the one made using PNG as the intermediate format. Dissecting that PDF reveals CCITT innards, which is the Huffman encoding typically in TIFF files.

(update) test results

  • NoLemurs
    link
    fedilink
    arrow-up
    7
    arrow-down
    1
    ·
    5 days ago

    This may be a limitation of the PNM format.

    Yup.

    PNM files don’t encode the physical dimensions. It is literally impossible for any program loading a PBM file to know what size it was originally, so any sane program is just going to fall back to a default resolution.

    • evenwicht@lemmy.sdf.orgOP
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      5 days ago

      It is literally impossible for any program loading a PBM file to know what size it was originally

      Woah. Impossible, really?

      $ exiftool -Comment='gimp user swore on their mother’s grave that this image is 8.27 inches wide' sample.pbm
      1 image files updated
      $ exiftool -Comment sample.pbm
      Comment                         : gimp user swore on their mother’s grave that this image is 8.27 inches wide
      

      I see potential.