Using ImageMagick's identify to determine DPI of a document
(Thanks to Tetsujin for his helpful comments leading me to the answer)

ImageMagick can get a little confusing with their myriad of options when you're attempting to do something.  There's a couple of ways that you can find out the DPI of an image.  Both of them use the identify command.

For the examples below, I'm using a sample, royalty free photo of an Apple iMac saved natively as a JPG and exported as a PNG.


Manually calculating the size.  To do this, we need two pieces of information, the resolution and the Units.  In this case, we lucked out because the the units are in PPI (pixels per inch) so the resolution makes sense - it's 72 PPI or 72 DPI 

identify -verbose apple_photo.jpeg | grep -E -i "resolution|units"                         
Resolution: 72x72
Units: PixelsPerInch


However, if the image file is a PNG, things are stored as PPCM or pixels per centimeter.  Issuing the same command:

% identify -verbose apple_photo.png | grep -E -i "resolution|units"
  Resolution: 28.34x28.34
  Units: PixelsPerCentimeter


This tells us that it's 28.34 PPCM.  To get DPI we have to do a little calculation.  1in = 2.54cm, so to get DPI we multiply that to the resolution as follows:

2.54 * 28.34 = 71.9836 or 72DPI






Real-time Calculation.  Again, using JPEG, we know that it's resolution is in DPI, so we just need to get the resolution value using the -format flag and the the percent (%) escape sequences:

identify -format "%[fx:resolution.x]" apple_photo.jpeg


Since PNG files are in PPCM,  we can use the calculated FX expressions and the percent (%) escape sequences, to calculate this value. Here, we're getting the resolution value and multiplying it by 2.54 to get DPI.

% identify -format "%[fx:resolution.x*2.54] DPI" apple_photo.png
71.9836 DPI


Note:  You can get the units using the %[units] escape sequence here:

% identify - identify -format `"%[units]"` apple_photo.jpeg



Calculate the size. We can now calculate the size (inches) of the image. Here, we use the %[w] escape sequence to get the width in DPI or PPCM

    % identify -format "%[fx:w]" apple_photo.jpeg
    3004


Divide by 72 to get inches

    % identify -format "%[fx:w/72] inches" apple_photo.jpeg
    41.7222 inches