Page 1 of 1

Getting file size in bytes

Posted: 2017-04-05T02:19:04-07:00
by locrianmode
How can I get the file size in bytes using convert or identify

E.g. running the command
identify -format "%b" some_file
or
convert some_file :json

returns "8.794MB" as the value or json filesize attribute

This would be 8794000 bytes, with the precision lost

I would like to have it returned in bytes e.g.
8794521

Thanks

Re: Getting file size in bytes

Posted: 2017-04-05T09:24:05-07:00
by fmw42
Note that depending upon the size of the file, you could get B or KB or MB or GB. So you will need to capture the result of identify and process by testing on the alphabetic characters. Offhand I cannot think of any other way to get around that.

Re: Getting file size in bytes

Posted: 2017-04-05T17:09:50-07:00
by fmw42
In Unix syntax, you can do this to parse the format and multiply by the appropriate equivalent:

Code: Select all

infile="woodtexture_large.png"
format=`identify -ping -format "%b" $infile | sed 's/[0-9.]*//g'`
val=`identify -ping -format "%b" $infile | sed 's/[A-Za-z]*//g'`
if [ "$format" = "B" ]; then
    size=$val
elif [ "$format" = "KB" ]; then
    size=`convert xc: -precision 15 -format "%[fx:round($val*1000)]" info:`
elif [ "$format" = "MB" ]; then
    size=`convert xc: -precision 15 -format "%[fx:round($val*1000000)]" info:`
elif [ "$format" = "GB" ]; then
    size=`convert xc: -precision 15 -format "%[fx:round($val*1000000000)]" info:`
fi
echo "size=$size"
size=20370000

Replace my image with yours. Note that this will still lose precision if the format is MB or GB

Please always provide your IM version and platform, since scripting syntax is different for Unix and Windows.

Re: Getting file size in bytes

Posted: 2017-04-05T19:18:28-07:00
by fmw42
P.S. How are you determining the size of the json file? Are you using IM or your OS file system? If the latter, then it is outside the domain of IM. If the former, please show the command.

Re: Getting file size in bytes

Posted: 2017-04-06T01:25:15-07:00
by snibgo
locrianmode wrote:How can I get the file size in bytes using convert or identify
I don't think IM can give the exact number of bytes. If that's what you want, the Unix tool "du" does that:

Code: Select all

f:\web\im>du -b toes.png

320268  toes.png