user11683992
user11683992

Reputation:

Is there a way to examine how much memory an image is occupying with python?

pillow provides size to examine the resolution of an image.

>> from PIL import Image
>> img = Image.open('Lenna.png')
>> img.size
(512, 512)

is there a way to examine how many memory the image is occupying? is the image using 512*512*4 Bytes memory?

Upvotes: 2

Views: 1595

Answers (2)

Nakor
Nakor

Reputation: 1514

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)

Upvotes: 1

Kai
Kai

Reputation: 124

import os
print os.stat('somefile.ext').st_size

or

import os
os.path.getsize('path_to_file.jpg')`

Upvotes: 1

Related Questions