Bytearray size on disk

1

Let's suppose I convert any image to bytearray .

with open("img.png", "rb") as imageFile:
  f = imageFile.read()
  b = bytearray(f)

print b[0]

How do I know how much disk space this bytearray will occupy if I decide to save it.

    
asked by anonymous 03.07.2018 / 22:22

1 answer

3

You can use the native len() function to calculate the size in bytes of a bytearray , see:

with open("img.png", "rb") as imageFile:
    f = imageFile.read()
    b = bytearray(f)

print(len(b))

Output:

384373

Comparing:

$ ls -al img.png 
-rw-rw-r-- 1 lacobus lacobus 384373 Jul  3 17:24 img.png
    
03.07.2018 / 22:30