Density does not work on some JPG images

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
david_o
Posts: 6
Joined: 2016-06-29T19:06:26-07:00
Authentication code: 1151

Density does not work on some JPG images

Post by david_o »

I'm using Ubuntu 16.04 and ImageMagick 6.8.9-9.

Here's an example of -density working correctly:

https://upload.wikimedia.org/wikipedia/ ... d_test.jpg
convert Image_upload_test.jpg -density 300 Image_upload_test2.jpg

When you check Image_upload_test2.jpg, the DPI is correctly set from 120 to 300

However, it doesn't work on this image:

http://render2.fotofuze.com/8680100E-3E ... 6AABD7.jpg
convert 8680100E-3E64-11E6-BF87-F5A2E46AABD7.jpg -density 300 8680100E-3E64-11E6-BF87-F5A2E46AABD7_2.jpg

The DPI doesn't change. Using Gimp to change the DPI and exporting the image will make ImageMagick's -density work again.

On another computer of mine that runs an older version of ImageMagick (6.7.7-10), this problem doesn't exist.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Density does not work on some JPG images

Post by GeeMack »

david_o wrote:However, it doesn't work on this image:

http://render2.fotofuze.com/8680100E-3E ... 6AABD7.jpg
convert 8680100E-3E64-11E6-BF87-F5A2E46AABD7.jpg -density 300 8680100E-3E64-11E6-BF87-F5A2E46AABD7_2.jpg
Checking that file using "ImageMagick 6.9.3-9 Q16 x64" on Windows 7 64 using this command...

Code: Select all

convert 8680100E-3E64-11E6-BF87-F5A2E46AABD7.jpg -verbose info:
... it appears it already has a 300x300 resolution. Here's the output...

Code: Select all

Image: 8680100E-3E64-11E6-BF87-F5A2E46AABD7.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 1567x1567+0+0
Resolution: 300x300
...
...
david_o
Posts: 6
Joined: 2016-06-29T19:06:26-07:00
Authentication code: 1151

Re: Density does not work on some JPG images

Post by david_o »

Other programs can't seem to identify the resolution as 300 (such as Gimp and programs trying to print the photo). Any idea why?
david_o
Posts: 6
Joined: 2016-06-29T19:06:26-07:00
Authentication code: 1151

Re: Density does not work on some JPG images

Post by david_o »

For example, if you open the file in Gimp, click Image -> Scale Image you'll see that the DPI is not 300. If you right click the file on Windows and select Properties -> Details you'll see the horizontal and vertical resolution is not 300. I've had people tell me that Photoshop can't detect that the resolution is 300 either.

I'm not suggesting that ImageMagick isn't writing the Exif data in such a way that it can't read it properly. I'm suggesting that ImageMagick is writing the Exif data in such a way that other programs can't read it properly. ImageMagick used to write things correctly, but it seems a bug has been introduced at some point.

I just installed 6.9.5-1 and the bug still exists.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Density does not work on some JPG images

Post by GeeMack »

david_o wrote:I'm not suggesting that ImageMagick isn't writing the Exif data in such a way that it can't read it properly. I'm suggesting that ImageMagick is writing the Exif data in such a way that other programs can't read it properly.
Try adding "-units PixelsPerInch" to your command line ahead of setting the "-density".

Code: Select all

convert 8680100E-3E64-11E6-BF87-F5A2E46AABD7.jpg -units PixelsPerInch -density 300 8680100E-3E64-11E6-BF87-F5A2E46AABD7_2.jpg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Density does not work on some JPG images

Post by fmw42 »

GeeMack is correct. Your input image has no defined units. So other programs do not know how to interpret the density. Is it pixels per inch or pixels per centimeter? If your units are undefined, it causes problems.

identify -verbose 8680100E-3E64-11E6-BF87-F5A2E46AABD7.jpg
Image: 8680100E-3E64-11E6-BF87-F5A2E46AABD7.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 1567x1567+0+0
Resolution: 300x300
Print size: 5.22333x5.22333
Units: Undefined
...

Providing the units along with the density should work properly.

Imagemgaick is likely assuming units of pixelsperinch to convert from resolution to print size. But other programs may not work correctly if the units are not specified.
david_o
Posts: 6
Joined: 2016-06-29T19:06:26-07:00
Authentication code: 1151

Re: Density does not work on some JPG images

Post by david_o »

I believe you are correct. Specifying the units makes it work.

It looks like the commit that broke things was made back on Sept 25, 2013: http://git.imagemagick.org/repos/ImageM ... a6e0801947

Image

You can see that the code defaults to density_unit to 1 (pixels per inch) at line 2129. But when the x_resolution and y_resolution are both not 0, the density_unit changes to 0 (undefined).

This is inconsistent with the ImageMagick docs which explicitly states "The default unit of measure is in dots per inch (DPI). The -units option may be used to select dots per centimeter instead." ( http://www.imagemagick.org/script/comma ... hp#density )

I will add the -units PixelsPerInch for now, but this really should be fixed as it's a bug. If there is a good reason why it is set to 0, then the docs really should be changed.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Density does not work on some JPG images

Post by magick »

The current release of ImageMagick does not set jpeg_info.density_unit to 0. Perhaps that was a transient bug. Here is the current state of JPEG density settings:

Code: Select all

  if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
    {
      /*
        Set image resolution.
      */
      jpeg_info.write_JFIF_header=TRUE;
      jpeg_info.X_density=(UINT16) image->resolution.x;
      jpeg_info.Y_density=(UINT16) image->resolution.y;
      /*
        Set image resolution units.
      */
      if (image->units == PixelsPerInchResolution)
        jpeg_info.density_unit=(UINT8) 1;
      if (image->units == PixelsPerCentimeterResolution)
        jpeg_info.density_unit=(UINT8) 2;
    }
david_o
Posts: 6
Joined: 2016-06-29T19:06:26-07:00
Authentication code: 1151

Re: Density does not work on some JPG images

Post by david_o »

Yes, currently units is set to 0 at line 1109, it gets the information from the Exif data, and sets the units to whatever the Exif data is currently set to.

Code: Select all

  units=0;
  if ((jpeg_info.saw_JFIF_marker != 0) && (jpeg_info.X_density != 1) &&
      (jpeg_info.Y_density != 1))
    {
      image->resolution.x=(double) jpeg_info.X_density;
      image->resolution.y=(double) jpeg_info.Y_density;
      units=(size_t) jpeg_info.density_unit;
    }
  if (units == 1)
    image->units=PixelsPerInchResolution;
  if (units == 2)
    image->units=PixelsPerCentimeterResolution;
Then later it sets the Exif data as was previoiusly set. (I'm assuming image->units is defaulted to 0, otherwise that would be a bug)

Code: Select all

  if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
    {
      /*
        Set image resolution.
      */
      jpeg_info.write_JFIF_header=TRUE;
      jpeg_info.X_density=(UINT16) image->resolution.x;
      jpeg_info.Y_density=(UINT16) image->resolution.y;
      /*
        Set image resolution units.
      */
      if (image->units == PixelsPerInchResolution)
        jpeg_info.density_unit=(UINT8) 1;
      if (image->units == PixelsPerCentimeterResolution)
        jpeg_info.density_unit=(UINT8) 2;
    }
This is all perfectly fine, except that the documentation states that unless specified that density_unit should always be 1 (PixelsPerInch). If this is no longer how ImageMagick wants to function, then the documentation should be updated with an errata stating that people using ImageMagick versions earlier than 7.0.2-3 will have PixelsPerInch as the default unless specified with the -units option.

This change in how ImageMagick functions is undocumented and may give developers upgrading from Ubuntu 14.04 to 16.04 headaches when they find out things have been silently failing and there's no official documentation as to why.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Density does not work on some JPG images

Post by fmw42 »

I believe a decision was made not too long ago. That was not to force an unknown density to units of dots per inch, but to leave it unspecified. I believe someone complained that units were being set wrongly for some images under those conditions. I could be wrong and defer to the IM developers.

However, I would agree that the whatever the situation, it should be properly documented.

see the changelog

2015-05-03 6.9.1-3 Dirk Lemstra <dirk@lem.....org>
...
Jpeg images no longer have pixels per inch as a default value for density units when the density is not set (reference viewtopic.php?f=3&t=27589).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Density does not work on some JPG images

Post by snibgo »

I suggest the documentation at http://www.imagemagick.org/script/comma ... hp#density has the following removed:
The default unit of measure is in dots per inch (DPI). The -units option may be used to select dots per centimeter instead.
Those sentences are misleading. Replace them by:
The -density setting should generally be used with the -units setting.
I don't think the documentation needs to define the behaviour for the four cases when there is no "-units" setting in the command. (Four cases: the input image has one of the three -units settings, or the input comes from somewhere like xc:). But it could, if someone wants to define that behaviour.
snibgo's IM pages: im.snibgo.com
david_o
Posts: 6
Joined: 2016-06-29T19:06:26-07:00
Authentication code: 1151

Re: Density does not work on some JPG images

Post by david_o »

The only other oddity I see is..

Code: Select all

convert 8680100E-3E64-11E6-BF87-F5A2E46AABD7.jpg 8.tga
convert 8.tga -verbose info:
This gives back:
Geometry: 1567x1567+0+0
Resolution: 72x72
Print size: 21.7639x21.7639
Units: Undefined
However, if you then convert the TGA back to a JPG:

Code: Select all

convert 8.tga 8.jpg
convert 8.jpg -verbose info:
Geometry: 1567x1567+0+0
Resolution: 72x72
Print size: 21.7639x21.7639
Units: PixelsPerInch
When using a format that doesn't support EXIF, the units change from Undefined to PixelsPerInch when converting it to JPG. It looks like PixelsPerInch IS the default, but only when converting from a format that doesn't support a units equivalent field.

This is an annoying discrepency. The TGA should return PixelsPerInch. Alternatively, converting a TGA to a JPG should keep the units as Undefined.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Density does not work on some JPG images

Post by snibgo »

Here are some other oddities:

Code: Select all

f:\web\im>%IM%convert xc: x.png & %IM%identify -verbose x.png |grep Units
  Units: Undefined

f:\web\im>%IM%convert xc: x.jpg & %IM%identify -verbose x.jpg |grep Units
  Units: Undefined

f:\web\im>%IM%convert xc: x.tga & %IM%identify -verbose x.tga |grep Units
  Units: Undefined

f:\web\im>%IM%convert xc: x.tiff & %IM%identify -verbose x.tiff |grep Units
  Units: PixelsPerInch
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Density does not work on some JPG images

Post by fmw42 »

Perhaps it has to do with which formats support "undefined" for units.
Post Reply