Getting file size in bytes

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
locrianmode
Posts: 1
Joined: 2017-04-05T01:50:15-07:00
Authentication code: 1151

Getting file size in bytes

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Getting file size in bytes

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Getting file size in bytes

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Getting file size in bytes

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting file size in bytes

Post 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
snibgo's IM pages: im.snibgo.com
Post Reply