2

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?

Nakor
  • 1,484
  • 2
  • 13
  • 23

2 Answers2

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

or

import os
os.path.getsize('path_to_file.jpg')`
Kai
  • 122
  • 6
1

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)
Nakor
  • 1,484
  • 2
  • 13
  • 23
  • thanks for your answer. is this very close to the RAM that the image is occupying actually? –  Jun 26 '19 at 03:12
  • Actually, after reading this website, this: https://code.tutsplus.com/tutorials/understand-how-much-memory-your-python-objects-use--cms-25609 , it seems to work mostly for primitive types. See my edits – Nakor Jun 26 '19 at 03:19