how to get de-facto bits-per-pixel image property via identify

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
AlexPUA
Posts: 5
Joined: 2017-11-09T22:48:47-07:00
Authentication code: 1152
Location: Ukraine

how to get de-facto bits-per-pixel image property via identify

Post by AlexPUA » 2017-11-10T02:15:18-07:00

The goal is to get value (file_size_in_bytes)/(image_height*image_width) in massive (Windows !) batch.
There are two problems (for me as a beginner):
- I found no -format option to get "always integer" filesize in bytes (%[size] give KB or MB values) to do math externally
- "obvios" calculations via fx:
identify -format "%[width] %[height] %[size] %[fx:b/(w*h)]" filename.jpg
gives uncorrect result because %b file size of image read in is not the file size but usage of %[size] impossible in fx:

snibgo
Posts: 9404
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to get de-facto bits-per-pixel image property via identify

Post by snibgo » 2017-11-10T03:11:24-07:00

%b and %[size] are strings, not numbers. I don't think IM can give you the size in bytes.
snibgo's IM pages: im.snibgo.com

AlexPUA
Posts: 5
Joined: 2017-11-09T22:48:47-07:00
Authentication code: 1152
Location: Ukraine

Re: how to get de-facto bits-per-pixel image property via identify

Post by AlexPUA » 2017-11-10T05:42:14-07:00

So, is there no solution inside IM at all ? Quite strange for such a powerful tool with all (trivial) data on the fingertips ...

User avatar
fmw42
Posts: 22107
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: how to get de-facto bits-per-pixel image property via identify

Post by fmw42 » 2017-11-10T13:00:21-07:00

You can test for KB or MB in the returned value and then multiply by 1000 or 1000000 and change KB or MB to B.

snibgo
Posts: 9404
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to get de-facto bits-per-pixel image property via identify

Post by snibgo » 2017-11-10T14:43:04-07:00

IM quite often doesn't provide facilities that are available elsewhere. IM is an image processor, not a file processor, so we often need to use script facilities to do what we want.

For example, in Windows CMD:

Code: Select all

for %F in (x*.jpg) do %IM%convert %F -format "%[fx:%~zF/w/h]" info:
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22107
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: how to get de-facto bits-per-pixel image property via identify

Post by fmw42 » 2017-11-10T15:33:54-07:00

What is your version of IM? On my 6.9.9.22 and 7.0.7.10 Q16 Mac OSX, I get B (bytes)

Code: Select all

convert barns_grand_tetons.jpg -format "%b\n" info:
836977B

Code: Select all

identify -format "%b\n" barns_grand_tetons.jpg
836977B

Code: Select all

identify -format "%[size]\n" barns_grans.jpg 
836977B

It has been this way since IM 6.9.8.4

AlexPUA
Posts: 5
Joined: 2017-11-09T22:48:47-07:00
Authentication code: 1152
Location: Ukraine

Re: how to get de-facto bits-per-pixel image property via identify

Post by AlexPUA » 2017-11-11T03:32:19-07:00

This code really works @ ImageMagick-7.0.3-Q16 and ImageMagick-7.0.7-Q16 (Windows-64)

Code: Select all

for %%F in (*.jpg) do magick convert "%%F" -format """%%f"" %%[fx:%%~zF/w/h]\n" info: >> files_info.txt

User avatar
fmw42
Posts: 22107
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: how to get de-facto bits-per-pixel image property via identify

Post by fmw42 » 2017-11-11T11:31:00-07:00

magick identify may be slightly faster than magick. You do not want to use magick convert. That points back to the IM 6 versions of convert. Just replace convert with magick. The other tools need to be prefaced with magick in IM 7

User avatar
fmw42
Posts: 22107
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: how to get de-facto bits-per-pixel image property via identify

Post by fmw42 » 2017-11-13T19:37:54-07:00

Note that you can use -precision to control whether you get B or KB or MB, but it round off the fractional part

Code: Select all

identify -format "%b\n" barns_grand_tetons.jpg
836977B

Code: Select all

convert -precision 3 barns_grand_tetons.jpg -format "%b\n" info:
837KB

Post Reply