myhd
myhd

Reputation: 332

Convert ImageMagick’s output from scientific to decimal?

I have a small one-liner in terminal which is to write the pixel count of many JPEG files to a text file:

find . -name *.jpg -exec convert {} -format "%[fx:w*h]" info: \; > sizes.txt

It actually does, but some of the numbers are in scientific notation, like here:

949200
960000
1.098e+06
1.038e+06
1.1664e+06
1.0824e+06
831600

What is the most robust / elegant way to have the commands output just in decimal notation, like in the following lines?

949200
960000
109806
103806
1166406
1082406
831600

I was wondering if you would do this within the ImageMagick fx part or rather pipe the output to another command for conversion. Thanks!

Upvotes: 3

Views: 853

Answers (3)

hackerb9
hackerb9

Reputation: 1912

To have ImageMagick print numbers larger than one million without truncating them to exponential (scientific) notation, use the -precision n option. The default precision is 6. I suggest setting it to 12, which allows numbers up to 1e+12 (a 1 followed by twelve zeros).

$ convert xc: -precision 12 -format '%[fx:1000*1000]\n' info:-
1000000

$ convert xc: -precision 6 -format '%[fx:1000*1000]\n' info:-
1e+06

Upvotes: 2

Levon
Levon

Reputation: 143022

This modification to the shell command seems to work:

find . -name *.jpg -exec convert {} -format "%[fx:w*h]" info: \;  | xargs printf "%0.0f\n" > sizes.txt

Adjust the formatting directives printf "%0.0f\n" to suit your needs.

Just for demonstration purposes (this works the same with find etc and .jpg files found on my system):

$ cat data.txt

949200
960000
1.098e+06
1.038e+06
1.1664e+06
1.0824e+06
831600

$ cat data.txt | xargs printf "%0.0f\n"

949200
960000
1098000
1038000
1166400
1082400
831600

Upvotes: 0

rwst
rwst

Reputation: 2675

According to http://www.imagemagick.org/script/escape.php there is no obvious way to get other number formats with the %fx: directive, so a command line solution is necessary.

Converting the w*h scientific notation output will lose you significant digits, so better output w and h separately and multiply.

Using bc this would be:

find . -name '*.jpg' -exec convert {} -format "%w*%h" info: \; |bc

Upvotes: 3

Related Questions