How to find pixels per inch (PPI) using ImageMagick

拈花ヽ惹草 提交于 2019-12-09 06:38:13

问题


I can get PPI for a JPEG image using the following command:

$ identify -format "%w x %h %x x %y" mypic.jpg 
1600 x 1200 72 PixelsPerInch x 72 PixelsPerInch

However, when I run the same command on PNG format I get Pixels Per Centimeter:

$ identify -format "%w x %h %x x %y" mypic.png 
1600 x 1200 28.35 PixelsPerCentimeter x 28.35 PixelsPerCentimeter

Is there a way to change the command to get Pixels Per Inch (PPI) for PNG format as well or perhaps calculate the PPI based on the pixels per centimeter?


回答1:


The resolution and units used are stored in the file, so if the resolution is stored in PixelsPerCentimeter, that's how identify will display it. There isn't a way to do the conversion automatically through identify. But it's just cm to inch conversion math:

PixelsPerInch = PixelsPerCentimeter * 2.54



回答2:


You can simply add the option

-units PixelsPerInch



回答3:


You can use the fx operator and some smart formatting in the output of identify like this:

identify -format "%[fx:int(resolution.x*2.54)]" image.png
299

Of course, the true joy of this is that it is platform-independent, so you don't have to shell out to dc on OS X and Linux, or do whatever muppetry is required on Windows to do the maths.




回答4:


1 inch =~ 2.54 cm. So it is trivial to convert between the two.



来源:https://stackoverflow.com/questions/14632254/how-to-find-pixels-per-inch-ppi-using-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!