2

resized_image = Image.resize((100,200));

Image is Python-Pillow Image class, and i've used the resize function to resize the original image,

How do i find the new file-size (in bytes) of the resized_image without having to save to disk and then reading it again

wolfgang
  • 7,281
  • 12
  • 44
  • 72

2 Answers2

3

The file doesn't have to be written to disk. A file like object does the trick:

from io import BytesIO

# do something that defines `image`...   
img_file = BytesIO()
image.save(img_file, 'png')
print(img_file.tell())

This prints the size in bytes of the image saved in PNG format without saving to disk.

BlackJack
  • 4,476
  • 1
  • 20
  • 25
  • 1
    You can use an alternative to `BytesIO` that simply counts without needing to store the output: http://stackoverflow.com/questions/13407717/python-image-library-how-to-compress-image-into-desired-file-size – Mark Ransom Apr 01 '15 at 02:56
0

You can't. PIL deals with image manipulations in memory. There's no way of knowing the size it will have on disk in a specific format.

You can save it to a temp file and read the size using os.stat('/tmp/tempfile.jpg').st_size

Sander van Leeuwen
  • 2,963
  • 2
  • 22
  • 31