How to read the dpi resolution of an image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
hape

How to read the dpi resolution of an image

Post by hape »

Been using IM for more than 10 years now as command line op called from php scripts.
Usually to convert and resize uploaded images, create thumbnails etc etc
Typical line - ("$CONVERT -geometry $himagesize -quality 95 $tmpname1 ".$M_IMAGES.$uniq_imageid."_1.jpg")
Lately users tend to upload high res images with 300 and above dpi.
Not a problem with my Firefox/LINUX ... but a problem for WIN users with IE. Depending on the version and the type of monitor they use, those hi res images are not displayed.
What I am trying to figure out is a new upload routine (just command line)
Read an uploaded image, get the dpi resolution and if not 72dpi convert to 72dpi while keeping the size in pixels.
Did not find a solution yet.
Any suggestion would be greatly appreciated.
Thanks for your help
Helmut
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to read the dpi resolution of an image

Post by fmw42 »

see string formats %x and %y and use -density to convert

convert image -format "%x x %y" info:

But dpi should have no bearing on image display as the display only depends upon the pixel resolution (width and height). DPI is only used when printing. You can get the width and height similarly from

convert image -format "%w x %h" info:

Windows uses see http://www.imagemagick.org/Usage/windows/
hape

Re: How to read the dpi resolution of an image

Post by hape »

Thanks. Will test it
Jimbo
Posts: 23
Joined: 2019-08-26T12:24:48-07:00
Authentication code: 1152

Re: How to read the dpi resolution of an image

Post by Jimbo »

Don't be too quick to dismiss dpi.

Photoshop, for example, has a built-in limitation. It can only correctly display images if the resulting x or y dimension in INCHES is less than 1000.

So, if you are using Photoshop as part of your process to completion, specifying dpi is vital to really large images. A dpi of 72 is rather small, and if the image is large 15K and above, you will be in some serious waiting to convert the result in Photoshop. Much better to use ImageMagick as long as it treats dpi correctly.

Currently, I have not found a way to change the density of an EXR image correctly using ImageMagick and Exr does support dpi for its header.

My input.exr image is 10240x4522 by 72dpi.

Here are the three calls I have tried this far to change an ex's dpi:

convert -density 180 input.exr output.exr

convert -units PixelsPerInch -density 180 input.exr output.exr

convert -units PixelsPerInch -density 180 -resize 10240 input.exr output.exr

None of them worked. The resulting output.exr image was the same as the input.

Thoughts? Suggestions?

Cheers.
- Jimbo
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to read the dpi resolution of an image

Post by fmw42 »

All your command are wrong as I mentioned above. Changing the density before reading a raster image works for vector formats. You should specify a density after reading the input raster. Try

convert input.exr -units PixelsPerInch -density 180 output.exr

If that does not work, then ImageMagick has no method of affecting the writing of density to EXR.

The density should not affect the display of raster images. It only affects printing. I have no idea why Photoshop would limit the density on raster images. That might be meaningful for vector files, only.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to read the dpi resolution of an image

Post by fmw42 »

Do you have OpenEXR installed such that imagemagick can find it and report it in

Code: Select all

convert -version
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to read the dpi resolution of an image

Post by fmw42 »

I do not see anything about density in the EXR documentation at https://www.openexr.com/documentation/R ... eFiles.pdf
Post Reply