I made the tests below using ImageMagick 7.0.3-0 Q8 x86 under Windows XP.
Background:
I need to periodically convert certain png images to jpeg, and I want the jpeg to have a specific resolution (dpi), different from that of the original image.
I do it with a batch script with ImageMagick, on a Windows XP system.
I just found out that for the past 4 years the dpi has been actually set incorrectly.
Luckily it also turned out that the setting wasn't really considered by the software using the images, so the thing got undetected.
What my problem led me to find out is that in ImageMagick's convert command if -units is left unspecified (which ought to be equivalent to setting it to PixelsPerInch, as per command-line-options.html#units) or explicitly set to PixelsPerInch between the input and output file names, e.g. :
Code: Select all
convert logo.png -units PixelsPerInch -density 100 logo.jpg
Instead the actual result is that the Units parameter is correctly 01, but Xdensity and Ydensity are set to 254 (x00FE)! It looks like the "100" was considered to be in PixelsPerCentimeter!!! (2.54 = measure of an inch in centimeters)
I noticed that I can get the results I want by setting -units before both file names; for example:
Code: Select all
convert -units PixelsPerInch -density 100 logo.png logo.jpg
Can someone explain me if this is expected behaviour or a bug, and if the former why it is so?
In the preceding examples I used the logo.png image distributed with ImageMagick, which has a resolution of 11811 dots per meter, that is 299,9994 dpi.
I checked the produced images directly with an hex editor, to be sure of what was being done (they are in jfif format, in which the Units parameter is at offset 0D (1 byte), Xdensity at 0E (2 bytes) and Ydensity at 10 (2 bytes) ).
Other interesting things I noticed:
Code: Select all
convert logo.png -units PixelsPerCentimeter -density 100 logo.jpg
So maybe the -units option is always considered for writing the jfif Units parameter, but is ignored when reading the -density argument if -units is absent or placed between the input and output file names?
But then why
Code: Select all
convert -units Undefined logo.png -density 100 logo.jpg
Note that I did notice that using "-set units PixelsPerInch" instead of "-units PixelsPerInch" gives me the expected results even when it's placed between the input and the output file names.
I would still like to understand if -units has a bug or I just misunderstood it, though.
Note:
Some images I made 5 years ago do have the correct dpi, and I do remember ensuring that the script worked when I first made it, so it might be a change introduced in ImageMagick at some point.
The script wasn't under version control though, so it's entirely possible that it was just using different commands at that time.