You could use the sys
library to get the size of an object in bytes. The difference with Kai's answer is that he's calculating the size of the image on the disk, while this calculates the size of the loaded python object (with all its metadata):
import sys
sys.getsizeof(img)
EDIT: After seeing this website, sys.getsizeof()
seems to work mainly for primitive types.
You could have a look at a more thorough implementation (deep_getsizeof()
) here .
This post gives also a lot of details.
And finally, there is also the pympler
library that provides tools to calculate the RAM memory used by an object.
from pympler import asizeof
asizeof.asizeof(img)