Page 1 of 1

Identify from ImageMagick prints output using wrong units.

Posted: 2010-04-23T00:55:51-07:00
by Petr
Original report at

https://bugzilla.novell.com/show_bug.cgi?id=598714

I tested it with several images, always the same problem.

comp> identify -units PixelsPerInch -verbose -format "%x %y %z" test2.png
236.22 PixelsPerInch 236.22 PixelsPerInch 8
comp> identify -verbose -format "%x %y %z" test2.png
236.22 PixelsPerCentimeter 236.22 PixelsPerCentimeter 8

Correct is:

laura:/> identify -verbose -units PixelsPerInch -format "%x %y %z" test2.png
599.999 PixelsPerInch 599.999 PixelsPerInch 8
laura:/> identify -verbose -format "%x %y %z" test2.png
236.22 PixelsPerCentimeter 236.22 PixelsPerCentimeter 8

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-04-23T01:03:12-07:00
by Petr
Please consider following patch. Maybe is stupid, I don't know the ImageMagick much to know all the context.

Code: Select all

Index: ImageMagick-6.6.1-0/magick/image.c
===================================================================
--- ImageMagick-6.6.1-0.orig/magick/image.c
+++ ImageMagick-6.6.1-0/magick/image.c
@@ -3959,6 +3959,9 @@ MagickExport MagickBooleanType SyncImage
   MagickStatusType
     flags;

+  ResolutionType
+    units;
+
   /*
     Sync image options.
   */
@@ -4150,16 +4153,18 @@ MagickExport MagickBooleanType SyncImage
       option);
   option=GetImageOption(image_info,"units");
   if (option != (const char *) NULL)
-    image->units=(ResolutionType) ParseMagickOption(MagickResolutionOptions,
+    units=(ResolutionType) ParseMagickOption(MagickResolutionOptions,
       MagickFalse,option);
-  if (image_info->units != UndefinedResolution)
+  else
+    units = image_info->units;
+  if (units != UndefinedResolution)
     {
-      if (image->units != image_info->units)
+      if (image->units != units)
         switch (image->units)
         {
           case PixelsPerInchResolution:
           {
-            if (image_info->units == PixelsPerCentimeterResolution)
+            if (units == PixelsPerCentimeterResolution)
               {
                 image->x_resolution/=2.54;
                 image->y_resolution/=2.54;
@@ -4168,7 +4173,7 @@ MagickExport MagickBooleanType SyncImage
           }
           case PixelsPerCentimeterResolution:
           {
-            if (image_info->units == PixelsPerInchResolution)
+            if (units == PixelsPerInchResolution)
               {
                 image->x_resolution*=2.54;
                 image->y_resolution*=2.54;
@@ -4178,7 +4183,7 @@ MagickExport MagickBooleanType SyncImage
           default:
             break;
         }
-      image->units=image_info->units;
+      image->units=units;
     }
   option=GetImageOption(image_info,"white-point");
   if (option != (const char *) NULL)

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-04-23T07:15:56-07:00
by magick
We can reproduce the problem you posted and have included your patch in ImageMagick 6.6.1-5 Beta available by sometime tomorrow. Thanks.

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-04-23T08:23:05-07:00
by fmw42
Perhaps I misunderstand the issue. But...

PNG only supports pixelspercentimeter. IM converts density from pixelsperinch if supplied that way to the correct density at pixelspercentimeter when creating a PNG.

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-04-24T00:26:32-07:00
by Drarakel
fmw42 wrote:PNG only supports pixelspercentimeter. IM converts density from pixelsperinch if supplied that way to the correct density at pixelspercentimeter when creating a PNG.
Yes, but there should be a way to recreate the 'PixelsPerInch value' IMO. When I have a 300dpi image, most programs do report the file as 300 dpi - even when it's stored with PixelsPerCentimeter internally. (Well, the Windows image info has some sort of rounding error there, because i see sometimes 299dpi when another software reports 300dpi.) And if I convert that file (with e.g. 118.11 PixelsPerCentimeter) to JPG, it gets written as 300 PixelsPerInch automatically with these programs. Which is what I want. (Even though I'm from Europe, and in other areas I'm used to centimeter - not to inches. :))
With the last ImageMagick versions, I couldn't accomplish that task. (Or did I miss something there?) I had to enter the density value manually again, as a "convert input.png -units PixelsPerInch output.jpg" command only changed the unit, but did not convert back the density value. (Which is basically the same issue that Petr reported.) And: It also did introduce another kind of 'rounding error' - as a simple "convert input.png output.jpg" command did change a 300dpi file (118.11 PPI) to a file that most programs did report as 299dpi (as the value is 118.0 PPI then in the JPG - of course I don't call that a bug).

Edit: Indeed, changing the unit/density works now with IM v6.6.1-5 (identify and convert). Thanks!

By the way (I think this is a bit related to this PixelsPerInch/PixelsPerCentimeter issue): It would be even better if the "Print size" could be output in inches AND centimeters (or be specified with an additional parameter). For example, if I have a 300dpi image that is approx. DIN A3 in size, I usually want to know the print size in centimeters (29,7x42cm), but the density in PixelsPerInch. Up till now I have to use two separate commands to get that.

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-04-24T15:12:25-07:00
by Drarakel
There is still some rounding issue IMO.
Example: Again, I have a 300dpi PNG. "identify -verbose" shows the correct info for that:

Code: Select all

  Resolution: 118.11x118.11
  Print size: 21.607x30.3361
  Units: PixelsPerCentimeter
"identify -verbose -units PixelsPerInch" correctly shows the converted values now:

Code: Select all

  Resolution: 299.999x299.999
  Print size: 8.50668x11.9434
  Units: PixelsPerInch
But if I convert that to a JPG with "convert input.png -units PixelsPerInch output.jpg", I get that:

Code: Select all

  Resolution: 299x299
  Print size: 8.53512x11.9833
  Units: PixelsPerInch
I think, the density should be mathematically rounded here (to 300x300), if the.. decimal places can't be kept. Is that possible?

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-04-24T16:39:10-07:00
by magick
We can reproduce the problem you posted and have included your patch in ImageMagick 6.6.1-6 Beta available by sometime tomorrow. Thanks.

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-04-24T20:08:41-07:00
by Drarakel
Great! Thanks! :)

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-05-08T12:17:46-07:00
by Drarakel
The rounding of the density values is better now (IM v6.6.1-6), thanks! But it doesn't work for all cases..

An example with a 96dpi PNG.. "identify -verbose" shows for that file:

Code: Select all

  Resolution: 37.8x37.8
  Print size: 21.5873x30.291
  Units: PixelsPerCentimeter
"identify -verbose -units PixelsPerInch" shows a correct value in inches:

Code: Select all

  Resolution: 96.01x96.01
  Print size: 8.49911x11.9258
  Units: PixelsPerInch
If I make a JPG with "convert input.png -units PixelsPerInch output.jpg", I get again fine values:

Code: Select all

  Resolution: 96x96
  Print size: 8.5x11.9271
  Units: PixelsPerInch
But with "convert input.png output.jpg", the resulting JPG shows this:

Code: Select all

  Resolution: 37x37
  Print size: 22.0541x30.9459
  Units: PixelsPerCentimeter
With "identify -verbose -units PixelsPerInch" on that file:

Code: Select all

  Resolution: 93.98x93.98
  Print size: 8.6827x12.1834
  Units: PixelsPerInch
So, a simple convert from PNG to JPG can 'change' the density from e.g. 96dpi to 93dpi.
Of course, there will always be a small loss of information with the conversions. But I think it would be better if the density was always rounded up mathematically when the result is written to a format with less precision. In the above example, the 37.8 ppc should be rounded to 38 ppc (and not 'cut off' to 37 ppc). I don't know though if that's easily possible.

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-05-08T12:51:57-07:00
by magick
We can reproduce the problem you posted and have included your patch in ImageMagick 6.6.1-8 Beta available by sometime tomorrow. Thanks.

Re: Identify from ImageMagick prints output using wrong unit

Posted: 2010-05-08T13:44:01-07:00
by Drarakel
magick wrote:6.6.1-8 Beta
That goes fast! :shock: Thanks! :)