Volker Siegel
Volker Siegel

Reputation: 3585

Can ImageMagick use specific number formats as in 'printf()' in the option '-format'?

I am using 'ImageMagick' command identify to calculate the area of images in pixels:

$ identify -format "%[fx:w*h]\n"  example-small.jpg 
699392

$ identify -format "%[fx:w*h]\n"  example-large.jpg
1.80497e+07

In the second case, the output is in the scientific format: 1.80497e+07. This is the case if the area is larger than some threshold (>= 1000000).

The option can be used with the 'ImageMagick' commands convert, identify and mogrify.

Is it possible to make the `%[fx: ... ]' format option use a specific number format in itself?

Upvotes: 1

Views: 250

Answers (1)

emcconville
emcconville

Reputation: 24419

Is it possible to make the `%[fx: ... ]' format option use a specific number format in itself?

Yes and No. When parsing %[fx: ... ] format, ImageMagick stores the result as double data type. This value will be passed to stdlib vsnprintf() method with a %.*g format. The output would result in the scientific notation for "extreme" numbers.

For the "yes" part. Since you're working with two unsigned integers (w*h), you may be able to spoof the behavior of vnsprintf() by setting the precision operator in ImageMagick.

identify -precision 32 -format "%[fx:w*h]\n"  example-large.jpg

However this is not a true "fix". It may not always work between systems, and adjusting the overall precision might have unforeseen side effects.

For the "no" part. It might be more practical & reliable to use an external utility to format the results to a format you can control.

printf "%.f" $(identify -format "%[fx:w*h]" example-large.jpg)

True that it is inconvenient, but a quick search for "[bash] scientific notation" offers many alternative ways as this a common task.

Upvotes: 2

Related Questions