Problems with -density if -unit PixelsPerInch not specificied before input image

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.
gabf
Posts: 4
Joined: 2016-09-13T05:41:20-07:00
Authentication code: 1151

Problems with -density if -unit PixelsPerInch not specificied before input image

Post by gabf »

I think this is a bug, but it might also be a misunderstanding on my part. It smells a lot like a bug though so I post it under Bugs anyway.


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
the results are not what I expect: I would expect that after the command logo.jpg would have a Units jfif parameter of 01 (dots per inch) and Xdensity and Ydensity of 100 (x0064).
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
gives me a logo.jpg with a Units parameter of 01 (dots per inch) and Xdensity and Ydensity of 100 (x0064).


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
works as expected, that is I get a logo.jpg with a Units parameter of 02 (dots per cm) and Xdensity and Ydensity 100 (x0064).
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
results in exactly the same image of the previous command, thus with a Units parameter of 02 (dots per cm) and Xdensity and Ydensity 100 (x0064) ???




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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by fmw42 »

For some images, the units are left unspecified and IM will not assume it to be pixels per inch. You must specify the units.
gabf
Posts: 4
Joined: 2016-09-13T05:41:20-07:00
Authentication code: 1151

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by gabf »

fmw42 wrote:For some images, the units are left unspecified and IM will not assume it to be pixels per inch. You must specify the units.
You seem to have read my post too hastily, please read it more carefully if you want to help.

The case of -units completely unspecified is just one of at least three that don't work as I expect.

The others are:
  • specifiying them, with value PixelsPerInch, after the input image name:

    Code: Select all

    convert logo.png -units PixelsPerInch -density 100 logo.jpg
  • specifying them with value Undefined (in any position), which in my tests results in a jfif Units parameter set to dots per cm (02) rather than to "no units" (00)
Even if there was just the case of completely unspecified -units, by the way, reading the -density as if it were in PixelsPerCentimeter but setting the output's jfif Units parameter to "dots per inch" seems a very odd behaviour, unlikely to be intentional.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by fmw42 »

This works for me both on IM 6.9.5-9 Q16 and 7.0.3-0 Q16 on my Mac OSX.

Code: Select all

identify -verbose logo:
Image: LOGO
Base filename:
Format: GIF (CompuServe graphics interchange format)
Mime type: image/gif
Class: PseudoClass
Geometry: 640x480+0+0
Units: Undefined

Code: Select all

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

Code: Select all

identify -verbose logo.jpg
Image: logo.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 640x480+0+0
Resolution: 100x100
Print size: 6.4x4.8
Units: PixelsPerInch


Sorry I know nothing about JFIF parameters, nor whether IM will set them. I do not see them listed in the IM verbose information. You will need to hear back from one of the IM developers or some one more knowledgable than me on that topic.
gabf
Posts: 4
Joined: 2016-09-13T05:41:20-07:00
Authentication code: 1151

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by gabf »

fmw42 wrote:You will need to hear back from one of the IM developers or some one more knowledgable than me on that topic.
Ok.

You're not a developer but you seem to know this forum well, so you can probably still advise me on this: is it likely that a developer reads this sooner or later, or I'd better repost it on github?

I posted it here because I wasn't 100% sure that it is a bug, but on second thought I'm probably confident enough to warrant a github issue.


fmw42 wrote: This works for me
[...]

Code: Select all

convert logo: -units pixelsperinch -density 100 logo.jpg
That works for me too, the problem occurs only with real files (at least with png ones), such as the logo.png that you can find in the "images" directory in your ImageMagick installation.

IM verbose seems to report the JFIF parameters correctly, they correspond to the Units and Resolution lines that you highlighted.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by fmw42 »

PNG only supports pixelspercentimeter. If you supply pixelsperinches, it will automatically be converted to the equivalent resolution in pixelspercentimeter.
gabf
Posts: 4
Joined: 2016-09-13T05:41:20-07:00
Authentication code: 1151

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by gabf »

fmw42 wrote:PNG only supports pixelspercentimeter. If you supply pixelsperinches, it will automatically be converted to the equivalent resolution in pixelspercentimeter.
What you're saying doesn't make much sense to me.
The first image supports only PixelsPerCentimeter, so the -density value specified for the second image is converted from PixelsPerInch to PixelsPerCentimeter, but the -units written is still PixelsPerInch??? This seems logical, expectable behaviour to you?


And what about the other thing I asked you?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by fmw42 »

IM 6.9.5-9 Q16 Mac OSX

Image: logo.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 2135x2048+0+0
Resolution: 118.11x118.11
Print size: 18.0764x17.3398
Units: PixelsPerCentimeter

Code: Select all

convert logo.png -units pixelsperinch -density 100 logo.jpg
Image: logo.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 2135x2048+0+0
Resolution: 254x254
Print size: 8.40551x8.06299
Units: PixelsPerInch


From the first case:
118.11 cm * 18.0764 pixelspercentimeter = 2135 pixels

From the second case:
8.40551 in * 254 pixelsperinch = 2135 pixel


This gives a proper equivalent result, but not at the density specified. We asked for a density of 100 pixelsperinch. That should have produced a resolution of 100 and units of pixelsperinch and a new print size of 213.5 x 204.8 inches

Perhaps I misunderstand how this automatic conversion is supposed to work. But it is not what I would have expected and I think it is wrong.



Another case of just changing the units and not the density.

Code: Select all

convert logo.png -units pixelsperinch logo.jpg
Image: logo.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 2135x2048+0+0
Resolution: 300x300
Print size: 7.11667x6.82667
Units: PixelsPerInch

I would have expected to see the resolution still at 118.11x118.11 with units of pixelsperinch and so a new print size of 18.08 x 17.34 inches. But we do not have that. The resolution was not preserved.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by fmw42 »

What I was saying before, is that if you have for example a JPG file with resolution 100 and units of pixelsperinch and convert that to a PNG file without specifying units or density, the result will be resolution will be 254 and units of pixelspercentimeter. If you specify density in pixels per inch and some other density, then it will automatically be converted to the equivalent resolution and pixelspercentimeter. PNG only supports pixels per centimeter.

Going the other way from PNG to JPG (or other formats), should properly convert from pixelspercentimeter to either pixelsperinch or pixelspercentimeter with the corresponding proper density.

But the latter does not seem to be working properly at this time as per my example above.

Also IM does not automatically convert unknown units to pixels per inch. So it really does not know what to produce for resolution and may produce something different from what you have for resolution. I do not know the code, so one of the developers would have to comment further. At one point in time, IM did assume pixelsperinch. But I think that was changed to ensure that the user specified exactly what he/she wanted.
vartlok
Posts: 5
Joined: 2018-08-14T03:57:55-07:00
Authentication code: 1152

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by vartlok »

Hello! It's still actual problem.
Version: ImageMagick 7.0.8-9 Q16 x86_64 2018-08-08

In my case I'm trying to convert JPG with PixelsPerCentimeter to JPG with PixelsPerInch and have the same problem.

Code: Select all

convert source.jpg -units PixelsPerInch -density 350 out.jpg
Resolution: 889x889

Code: Select all

convert -units PixelsPerInch -density 350 source.jpg out.jpg
Resolution: 350x350
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by snibgo »

You've quoted the resolution but not the units. I note that 350 dpi = 889 dpcm.
snibgo's IM pages: im.snibgo.com
vartlok
Posts: 5
Joined: 2018-08-14T03:57:55-07:00
Authentication code: 1152

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by vartlok »

Yep, I agree that 350 dpi = 889 dpcm.
But in both case:

Units: PixelsPerInch

Code: Select all

convert source.jpg -units PixelsPerInch -density 350 out.jpg
Units: PixelsPerInch
Resolution: 889x889

Code: Select all

convert -units PixelsPerInch -density 350 source.jpg out.jpg
Units: PixelsPerInch
Resolution: 350x350
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by snibgo »

I can't reproduce your problem on IM v7.0.8-10. Please link to your source.jpg.
snibgo's IM pages: im.snibgo.com
vartlok
Posts: 5
Joined: 2018-08-14T03:57:55-07:00
Authentication code: 1152

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by vartlok »

You can get source.jpg here
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problems with -density if -unit PixelsPerInch not specificied before input image

Post by snibgo »

vartlok wrote:convert -units PixelsPerInch -density 350 source.jpg out.jpg
What do you expect this command to do? Commands should be given in the order you want them executed: read the image, change the densaity, write the image. You can't change the density of an image before you have read the image.

Setting "-density" before reading an image makes sense when the image to be read has dimensions in inches and not pixels, eg an SVG image. Then the rasterizer needs to know the density so it can calculate how many pixels are needed.
snibgo's IM pages: im.snibgo.com
Post Reply