Many problems with convert command with png's

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
markm

Many problems with convert command with png's

Post by markm »

There seems to be some pretty serious inconsistencies and bugs with the convert command when it saves out to PNG files. These problems do not exist when outputing to a JPEG or TIFF.


TEST CASE #1:

Here is the same convert command being run on an input file (in.png). The only difference between the commands is the output file type. The output to TIFF and JPEG seems to do roughly what is expected. But the PNG output does nothing.

Code: Select all

identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /in.png

convert -units PixelsPerInch /in.png /out.tiff
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.tiff

convert -units PixelsPerInch /in.png /out.jpeg
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.jpeg

convert -units PixelsPerInch /in.png /out.png
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.png
EXPECTED RESULT:

1. Change the existing unit from PixelsPerCentimeter to PixelsPerInch.
2. Convert the existing x and y resolution values to be proportional to the new unit.
3. Write the new unit and resolution into the output file headers.

ACTUAL RESULT:

The first 2 convert commands work fine (though the converted resolution is less accurate with the JPEG conversion).

The command that converts to a PNG does not do anything at all.


TEST CASE #2:

Here is a convert command that all 3 output file types do fine. Just for reference. Again, the only difference between these commands is the output file type. These all do what is expected.

Code: Select all

identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /in.png

convert -density 72 /in.png /out.tiff
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.tiff

convert -density 72 /in.png /out.jpeg
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.jpeg

convert -density 72 /in.png /out.png
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.png
EXPECTED RESULT:

1. Replace the existing resolution X and Y headers with the new value (72).
2. Store the existing unit into the out, ignoring it completely.

ACTUAL RESULT:

Same as expected result.


TEST CASE #3:

Code: Select all

identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /in.png

convert -units PixelsPerInch -density 72 /in.png /out.tiff
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.tiff

convert -units PixelsPerInch -density 72 /in.png /out.jpeg
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.jpeg

convert -units PixelsPerInch -density 72 /in.png /out.png
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.png
EXPECTED RESULT:

1. Replace the existing units (PixelsPerCentimeter) with the new unit (PixelsPerInch).
1. Replace the existing resolution X and Y headers (28.35) with the new value (72).
3. Perform NO conversions, just save those new values into the output file headers.

ACTUAL RESULT:

The first 2 convert commands work fine. But the one saving to a PNG does nothing. It just basically saves a copy of the existing file. But if you type the -unit option after the -density option instead of before you at least get the new resolution written into the output PNG, but the unit remains unchanged. So:


TEST CAST #4:

Code: Select all

identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /in.png

convert -density 72 -units PixelsPerInch /in.png /out.png
identify -format "\n\n%f: %w by %h Pixels  %x by %y\n\n" /out.png
EXPECTED RESULT:

Same as test case #3.

1. Replace the existing units (PixelsPerCentimeter) with the new unit (PixelsPerInch).
1. Replace the existing resolution X and Y headers (28.35) with the new value (72).
3. Perform NO conversions, just save those new values into the output file headers.

ACTUAL RESULT:

The units are not changed.


The only other thing I would add is that the formats should all perform the PixelsPerCentimeter to PixelsPerInch conversion with exactly the same accuracy in test case #1. It is an inaccurate conversion by nature (multiplying by about 0.3937007874015748), but the JPEG conversion should at least be doing it to the same number of decimal places as the TIFF (and as many decimal places as possible for both).

Thanks. Hope to hear back from you,
Mark
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Many problems with convert command with png's

Post by magick »

The PNG image format does not support pixels-per-inch units, only pixels-per-centimeter. We're using the latest ImageMagick release and your image returns the expected results of
  • out.png: 640 by 480 Pixels 28.34 PixelsPerCentimeter by 28.34 PixelsPerCentimeter
markm

Re: Many problems with convert command with png's

Post by markm »

Hi,

I vote there should be an appropriate error message generated, and nothing should be outputted for any of the test cases. Ultimately that would be the expected result. And test #4 should do nothing (not even change the resolution), and generate the same "The PNG file format can only save resolutions in PixelsPerInch" message. Why #4 changes the resolution just because the options came in a different order should probably be investigated.

Would also be good if the TIFF and JPEG would convert from PixelsPerCentimeter to PixelsPerInch to the same level of precision. Perhaps there is a format reason for that as well?

Is it ImageMagick's usual behavior to generate images even when the options are faulty? Shouldn't it have generated an error message?

Mark
markm

Re: Many problems with convert command with png's

Post by markm »

I just made up a unit to see what ImageMagic would do:

Code: Select all

convert -density 72 -units PixelsPerSomethingStupid /in.png /stupid.png
it did not produce an image. Just displayed an error:
unrecognized units type `PixelsPerSomethingStupid'.
So the commands I gave that tried to save PixelsPerInch into a PNG should do exactly the same thing. But wording should maybe be:
Unrecognized units type `PixelsPerSomethingStupid'. PNG specification only supports units type PixelsPerCentimeter.
Of course, if someone tries to enter a recognized unit like 'PixelsPerInch' just the second part of that message should be displayed.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Many problems with convert command with png's

Post by anthony »

The problem is that -units PixelsPerInch is a correct setting. As such it is accepted and not errored. IM has no way of knowing that that setting will eventually attempt to save into an PNG image file format (it might only be a temporary thing).

Simularly when PNG coder tries to save, it has no way of knowing that the current units setting was user provided, verses something that came with a image that was read in. So it does the best it can in the situation given

In neither place can a fatal error be generated, so IM does what it thinks in the right thing.


Your PixelsPerSomethingStupid error is a syntax error for the Command Line API. It is of course fatal and is discovered in the initial parse check of the command line. It is of course fatal.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply