Problem with FX expression %[fx:n]

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
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Problem with FX expression %[fx:n]

Post by GeeMack »

Using IM 7.0.8-53 Q16 x64 HDRI on Windows 10. The FX expression "%[fx:n]" gives incorrect results when there are no images in the list. Here is an example command...

Code: Select all

magick logo: -duplicate 5 -delete 0--1 -print "%[fx:n]" null:
That should show the number of images in the list as "0", but instead incorrectly reports the number of images as "1".

The issue also occurs when using the "n" by itself inside square brackets as in this command...

Code: Select all

magick logo: -duplicate 5 -delete 0--1 -print "%[n]" null:
That reports "1" image, but it should show "0". Interestingly, without the square brackets it correctly reports "0" images, like this...

Code: Select all

magick logo: -duplicate 5 -delete 0--1 -print "%n" null:
The issue also occurs in version IM 6.9.10-11 Q16 x64 HDRI. I have no idea which version(s) were first to have this problem.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Problem with FX expression %[fx:n]

Post by magick »

ImageMagick is behaving properly. Querying properties requires an image container. In some cases, the user demands a property even if an image is not available so one is automagically created. For example, you might want to perform a FX calculation before an image is read:

Code: Select all

magick -print "%[fx:.8765/3.14]" null: null:
Rather than return an exception, an image is instead created to ensure the FX expression can be parsed and a value returned. The correct fix to this problem, is to update the documentation so users are aware of this behavior.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Problem with FX expression %[fx:n]

Post by GeeMack »

magick wrote: 2019-07-14T12:38:30-07:00 ImageMagick is behaving properly. Querying properties requires an image. In some cases, the user demands a property even if an image is not available so one is automagically created. For example, you might want to perform a FX calculation before an image is read, for example,

Code: Select all

magick -print "%[fx:.8765/3.14]" null: null:
Thanks for the explanation. I should have known that.

I'm working on commands using FX expressions to "-set option:variable" to the number of images currently in the list. Then I'll use that variable later in the command for duplicating or deleting. I can get what I need by creating a placeholder "xc:", setting the variable to the length of the list minus 1, then deleting that placeholder, something like this...

Code: Select all

... xc: -set option:list_length %[fx:n-1] +delete ...
Thanks again!
Post Reply