Get the ppi(pixel per inch) of image using C API

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
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Get the ppi(pixel per inch) of image using C API

Post by him21sri »

I am currently using ImageMagick 6.8.9-10 version on linux. I want to get the image's ppi, is there a way to get this info using C api. Please help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get the ppi(pixel per inch) of image using C API

Post by snibgo »

MagickWand or MagickCore? I suggest you grep the *.h in the appropriate directory for "resolution". That should answer your question.
snibgo's IM pages: im.snibgo.com
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Get the ppi(pixel per inch) of image using C API

Post by him21sri »

Hey now I am a little confused of what I am doing. I will try to describe the scenario. Say I have an image of some dimension 100x100 pixels, I want to place some text in the center of this image and I want the text to cover 50% of the image's height leaving 25% space from both top & down, which is nothing 50px height for the text. Now I am using ImageMagick API DrawSetFontSize which expects the size to be in pointsize. Now I want to convert our 50px into corresponding font size so that I can write text of the same size. For calculation, we already know that 1pt = 1/72 inch and if we somehow know the ppi of an image then we can convert our pixels into points and then I can set the same calculated points in DrawSetFontSize to get what I want.

An example to illustrate how I am converting pixels to point. Consider the image is of 144ppi.

144 px = 1 inch
1 px = 1/144 inch
1 px = (1/144) * 72 pt (1pt = 1/72 inch)
1 px = 1/2 pt
50 px = 25 pt

So I can set 25 in DrawSetFontSize. The problem here is how to figure out the ppi of an image.

Am I in the right direction? If not then suggest how can I set the font size so that it covers only 50% area of image.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get the ppi(pixel per inch) of image using C API

Post by snibgo »

You didn't answer my question "MagickWand or MagickCore?" As DrawSetFontSize() is a MagickWand function, I'll assume you are using that.

Did you grep for "resolution" in wand\*.h? If you did, you would find the functions MagickGetImageResolution() and MagickGetImageUnits() in magick-image.h.

Then, look at these functions in magick-image.c for further detail.

If the x- and y-resolutions are different, the pixels are not square. This is rare.
him21sri wrote:The problem here is how to figure out the ppi of an image.
The answer is: use MagickGetImageResolution() and MagickGetImageUnits().
snibgo's IM pages: im.snibgo.com
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Get the ppi(pixel per inch) of image using C API

Post by him21sri »

Sorry for not mentioning the API I am using, You assumed correctly that I am using MagickWand functions.

I used MagickGetImageResolution but I am getting result as 0.000000 x 0.000000. Please find my sample code below.

double xx,yy;
MagickGetImageResolution(wand, &xx, &yy);
printf("ppi returned %lf x %lf", xx, yy);

And I am not able to figure out whats wrong. And please also clarify if my approach is ok as mentioned in the above post for figuring out the fontsize
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get the ppi(pixel per inch) of image using C API

Post by snibgo »

And what are the resolution units from MagickGetImageUnits() ? I expect they are "UndefinedResolution". So that image doesn't have a defined resolution. In such circumstances, IM assumes 72 dpi.
him21sri wrote:And please also clarify if my approach is ok as mentioned in the above post for figuring out the fontsize.
Yes, it is ok, but pointsize is the overall height of a text line (including the gap to the next line), not the height of any particular character.
snibgo's IM pages: im.snibgo.com
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Get the ppi(pixel per inch) of image using C API

Post by him21sri »

Yes the resolution type was 0 which is "UndefinedResolution". Even after setting PixelsPerInchResolution as the ImageUnits the result was still 0x0.

But when I do

identify -format "%xx%y" grass.png

I get the result as 118.10999999999999943x118.10999999999999943
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Get the ppi(pixel per inch) of image using C API

Post by him21sri »

Please help here as I am stuck because I am able to get the ppi from console command but not through the C API.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get the ppi(pixel per inch) of image using C API

Post by snibgo »

him21sri wrote:Please help here as I am stuck because I am able to get the ppi from console command but not through the C API.
Please show your program. The complete source code of a minimal program, not merely a fragment.

For me, with somewhat old versions of IM, the two functions and "identify -format "%%x x %%y %%U\n" in.ext" give the same result, except that "identify" reports "0x0" as "72x72 Undefined".
snibgo's IM pages: im.snibgo.com
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Get the ppi(pixel per inch) of image using C API

Post by him21sri »

Ok I have an question, so if for an image there is undefined resolution then can I assume the ppi to be 72 and do the above calculations for calculating the pointsize which is nothing but 1px = 1pt in this case. Will that be a correct way to do it for images which don't have a resolution defined? I mean will it cover the 50% area if I mention the fontsize as 50 pt(because 1px = 1pt, as I am assuming 72 ppi)? (this calculation is in refrences to my second comment in the post)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get the ppi(pixel per inch) of image using C API

Post by snibgo »

Yes.
snibgo's IM pages: im.snibgo.com
shoansa
Posts: 15
Joined: 2017-07-18T00:12:17-07:00
Authentication code: 1151

Re: Get the ppi(pixel per inch) of image using C API

Post by shoansa »

him21sri wrote: 2017-05-17T05:50:12-07:00 Yes the resolution type was 0 which is "UndefinedResolution". Even after setting PixelsPerInchResolution as the ImageUnits the result was still 0x0.

But when I do

identify -format "%xx%y" grass.png

I get the result as 118.10999999999999943x118.10999999999999943
Hey him21sri,

You can use below for .png format. This gave me best result for me on Windows7

identify -format "%[fx:int(resolution.x*2.54)]" input.png
shoansa
Posts: 15
Joined: 2017-07-18T00:12:17-07:00
Authentication code: 1151

Re: Get the ppi(pixel per inch) of image using C API

Post by shoansa »

Hi,

Can anyone help me to know that, is it possible to check "Resolution" of .ai, .eps, .pdf and .svg by IM command?

I am Windows user
IM version: "ImageMagick-6.9.1-Q16-HDRI"

Please help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get the ppi(pixel per inch) of image using C API

Post by snibgo »

Those are all vector formats, not raster, so what do you mean by "resolution"? This normally mean pixels per unit length (inch, centimetre, etc). But vectors don't have pixels.

"identify verbose" will give a resolution, but that is the resolution of the image after converting to a raster image.
snibgo's IM pages: im.snibgo.com
Post Reply