"-density" isn't working properly anymore

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
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

"-density" isn't working properly anymore

Post by Drarakel »

This issue came with IM version v6.6.3-1. In version 6.6.3-0, everything's working.
See here in the Users section: viewtopic.php?f=1&t=16752

Simple example:

Code: Select all

convert rose: rose.jpg
convert rose.jpg -density 123 roseb.jpg
identify -verbose roseb.jpg
...
Resolution: 72x72


If you need more details:
One problem is that "-density" doesn't work at all if specified after the input image (which was ok for many many versions), but only if specified before the input image. But the main problem is that it works only if the input doesn't has already a density value. So, at the moment one basically has to strip all the metadata or temporarily write the file to a format that can't store the density if one wants to change the density value.
This doesn't work:

Code: Select all

convert rose: TIFF:- | convert -density 123 - -format "%x x %y" info:-
72 Undefined x 72 Undefined
But this works (as "-density 0" doesn't create the EXIF tags in this case):

Code: Select all

convert -density 0 rose: TIFF:- | convert -density 123 - -format "%x x %y" info:-
123 PixelsPerInch x 123 PixelsPerInch

By the way: "-units" is affected, too. Up to version 6.6.1-4, specifying units and density after the input image simply changed these values. After version 6.6.1-5, specifying "-units" this way also converted the density value. (See here. Thanks again for that feature - it's working great!) But simply specifying units AND density after the input image (for a manual change) could result in wrong values then. No big problem so far, because one could specify the units value before the input image for a manual change. (But perhaps a hint to this could be added to the documentation.)
But now (version 6.6.3-1), even specifying it before the input image results in a conversion of the density value. So, it seems that a change of the units value alone is not possible anymore.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: "-density" isn't working properly anymore

Post by magick »

We can reproduce the problem you posted and have a patch in ImageMagick-6.6.3-2 Beta available by sometime tomorrow. Thanks,
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "-density" isn't working properly anymore

Post by fmw42 »

This part appears to be working in IM 6.6.3.2 Q16 Mac OSX Tiger

convert logo: -density 150 -units pixelsperinch logo.jpg

identify -verbose logo.jpg

Image: logo.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Class: DirectClass
Geometry: 640x480+0+0
Resolution: 150x150
Print size: 4.26667x3.2
Units: PixelsPerInch
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: "-density" isn't working properly anymore

Post by Drarakel »

Yes, "-density" is working again. And specifying density AND units for a manual change works, too (if units is specified before the input image). So it's like in version 6.6.3-0 again.
Thank you!

But there's still an issue with "-units" alone, I think:
Because simply changing the units from PixelsPerInch to PixelsPerCentimeter (or vice versa) is not possible, as there's no way to turn off the feature in "-units" (from version 6.6.1-5) which is converting the density values. (I was wrong here - changing units alone wasn't possible in version 6.6.3-0 either. I guess I had always used this in conjunction with a new density value.)
Example:

Code: Select all

convert -units PixelsPerCentimeter rose: -density 96 rose1.jpg
identify -format "%x x %y" rose1.jpg
96 PixelsPerCentimeter x 96 PixelsPerCentimeter

Now I want to change units to PixelsPerInch only:

Code: Select all

convert -units PixelsPerInch rose1.jpg rose2.jpg
identify -format "%x x %y" rose2.jpg
244 PixelsPerInch x 244 PixelsPerInch

Should be: 96 PixelsPerInch x 96 PixelsPerInch
So, is there a way to change the units value alone?
(Maybe with "-set"? The documentation suggests using "-set density" for the density value - but that option doesn't work. I thought that I once was able to use "-set units", but I can't recreate it - maybe my memory tricks me.)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: "-density" isn't working properly anymore

Post by magick »

Look for support of -set density / units in ImageMagck 6.6.3-3 Beta available by sometime tomorrow. Thanks.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: "-density" isn't working properly anymore

Post by Drarakel »

Thank you!


While I'm on it:
There are still some rounding issues with the density values. I hope I don't bug you with these rounding issues. :)

1) First - a very small one - but not unimportant, and perhaps it's relatively easy to fix:
As example, I create a picture with 72 dpi - that IM has to convert to PixelsPerCentimeter when storing as BMP or PNG:

Code: Select all

convert rose: -units PixelsPerInch -density 72 rose.bmp
identify -format "%x x %y" rose.bmp
28.34 PixelsPerCentimeter x 28.34 PixelsPerCentimeter

Code: Select all

identify -format "%x x %y" -units PixelsPerInch rose.bmp
71.98 PixelsPerInch x 71.98 PixelsPerInch

So far, so good.. Now I don't know exactly how IM currently does the conversion from PixelsPerInch to PixelsPerCentimeter (or vice versa), but I would do it like that (and sometimes I really do the math myself and specify the density values manually if I want to avoid rounding errors from IM):
72 / 2.54 * 100 = 2834.6... = 2835 = 0xB13
But in the BMP (or PNG, it's the same in this format) is the value 0xB12 (well, with little endian in BMP, it's 0x120B), which converts to 71.98 dpi - instead of 72.009 dpi with 0xB13. ImageMagick probably just stores the result of the division as integer value. But perhaps there could be another rounding before that (either with a normal floating point division and rounding or with tricks like '((72 * 100000 / 254 +5) /10)'? This would improve the precision and also improve the displaying of these values in tools that can't round at all (like Windows Explorer, that always displays plain '71 dpi' in images with PixelsPerInch that got created by ImageMagick). Tools like IrfanView do this kind of rounding (and store the value as 0xB13 in this example). :)

2) Take this JPG (with an EXIF profile):
Image

Code: Select all

identify -format "%x x %y" rose.jpg
300 PixelsPerInch x 300 PixelsPerInch

Code: Select all

convert rose.jpg -units PixelsPerCentimeter rose2.jpg
identify -format "%x x %y" rose2.jpg
118 PixelsPerCentimeter x 118 PixelsPerCentimeter

Code: Select all

identify -format "%x x %y" -units PixelsPerInch rose2.jpg
299.72 PixelsPerInch x 299.72 PixelsPerInch

ImageMagick does update the values in the EXIF profile and the JFIF header. But the loss of precision (300 PixelsPerInch should be at least 118.11, not 118.0 PixelsPerCentimeter) could be avoided if the value in the EXIF profile would be written as e.g. 1181102/10000 - instead of 118/1. (Not sure if it's worth the effort though.)
Post Reply