[Fixed]-Django ImageKit and PIL

1👍

use os.stat on the actual path of the file to get the size in bytes, then divide by 1024 to get KB:

import os
filesize = os.stat('/path/to/somefile.jpg').st_size
print filesize/float(1024)

0👍

The size in bytes will vary depending of the format in which the image will be saved. For example if you use highly compressed JPEG (low quality) the image will be smaller than PNG.

If you want to see the size before save it to file you can save it to in memory file and then get the size.

from io import BytesIO

class CustomCompress(object):
    def process(self, image):
        jpeg_file = BytesIO()
        png_file = BytesIO()

        image.save(jpeg_file, format='JPEG')
        image.save(jpeg_file, format='PNG')

        jpeg_size = len(jpeg_file.getvalue())
        png_size = len(png_file.getvalue())

        print('JPEG size: ', jpeg_size)
        print('PNG size: ', png_size)

Leave a comment