I have a large collection of documents scanned into PDF format, and I wish to write a shell script that will convert each document to DjVu format. Some documents were scanned at 200dpi, some at 300dpi, and some at 600dpi. Since DjVu is a pixel-based format, I want to be sure I use the same resolution in the target DjVu file as was used for the scan.
Does anyone know what program I can run, or how I can write a program, to determine what resolution was used to produce a scanned PDF? (Number of pixels might work too as almost all documents are 8.5 by 11 inches.)
Clarification after responses: I'm aware of the difficulties highlighted by Breton, and I'm willing to concede that the problem in general is ill-posed, but I'm not asking about general PDF documents. My particular documents came out of a scanner. They contain one scanned image per page, same resolution each page. If I convert the PDF to PostScript I can poke around by hand and find pixel dimensions easily; I could probably find image sizes with more work. And if in desperate need I could modify the dictionary stack that gs
is using; long ago, I wrote an interpreter for PostScript Level 1.
All of that is what I'm trying to avoid.
Thanks to help received, I've posted an answer below:
- Extract the bounding box from the PDF using
identify
, taking only the output for the first page, and understanding that the units will be PostScript points, of which there are 72 to an inch. - Extract images from the first page using
pdfimages
. - Get height and width of image. This time
identify
will give number of pixels. - Add the total areas of the images to get the number of dots squared.
- To get resolution, compute areas of bounding box in inches squared, divide dots squared by inches squared, take the square root, and round to the nearest multiple of 10.
Full answer with script is below. I'm using it in live fire and it works great. Thanks Harlequin for pdfimages
and Spiffeah for the alert about multiple images per page (it's rare, but I've found some).
Apago's PDF Spy will tell you the acutal resolution of images in a PDF along with lots of other stuff. It's a commercial product but has a 10 day demo.
If a pdf has been created by scanning then there should only be one image associated with each page. You can find each image resolution for each page image by parsing the pdf using the iText(Java) or iTextSharp(the .net port) libraries easily.
If you want to roll your own utility to do this, do something like the following in iTextSharp :
Here for each page we read through each XObject of subtype Image and get the WIDTH and HEIGHT values. This will be the pixel resolution of the image that the scanner has embedded in the pdf.
Note that the scaling of this image to match the page resolution (as in the size of the page rendered in Acrobat - A4, Letter, etc) is performed separately in the page content stream, which is represented as a subset of postscript, and much harder to find without parsing the postscript.
Be aware that there are some scanners that will embed the scanned image as a grid of smaller images (for some kind of size optimization I assume). So if you see something like 50 small images popping up for each page, that could be why.
Hope this helps in some way if you have to roll your own utility.
pdfimages
has a-list
option that gives the height width in pixels and alsoy-ppi
andx-ppi
.I guess that the scans are included as images in the PDF, so you could use
pdfimages
to extract them first. Then,identify
should be able to find the correct data.PDF is a resolution independent format, it's a nonsensical question. You may have scanned some bitmaps at a particular resolution, and those bitmaps are individually embedded inside the pdf, but the PDF itself may contain images at multiple resolutions, as well as resolution independent vector graphics. There's no way to know without cracking open the pdf and examining every object inside it.
Editing to continue expounding on the problem:
You may have gotten lucky, and the software you used to scan the documents embedded some metadata about this, but don't bet on it. Such metadata is unlikely to be standard. As far as parsing the pdf, you'd want a prewritten library - such as ghostscript. The problem is that PDF isn't really a format so much as it is a specified subset of the PostScript programming language, and an agreed upon way of compressing/compiling this subset along with some binaries. Thus reading a PDF is more complicated than other types of image formats, as it involves writing a language interpreter - not so straightforward.
The best approach is to either throw up your hands and give up, or really look hard at ghostscript and see if you can get that to tell you the answer.
Too long to put into a comment, but neither ImageMagick nor GraphicsMagic is up to the job; every answer is wrong:
The correct parameters for this document is that each scanned page is 5100 pixels wide and 6600 pixels high, unsurprising for this was an 8.5-by-11 scanned at 600dpi. The output from ImageMagic is astoundingly unprofessional.
No downvotes because you were trying to be helpful, but
*Magick
don't work.