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
Getting file size in bytes
-
- Posts: 1
- Joined: 2017-04-05T01:50:15-07:00
- Authentication code: 1151
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Getting file size in bytes
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Getting file size in bytes
In Unix syntax, you can do this to parse the format and multiply by the appropriate equivalent:
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.
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"
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Getting file size in bytes
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Getting file size in bytes
I don't think IM can give the exact number of bytes. If that's what you want, the Unix tool "du" does that:locrianmode wrote:How can I get the file size in bytes using convert or identify
Code: Select all
f:\web\im>du -b toes.png
320268 toes.png
snibgo's IM pages: im.snibgo.com