Skip to content
Advertisement

What is the correct approach to display the Image size in Python?

Using the idea from this code for changing image size dynamically in a loop, there’s bit of a problem here. There are a couple of methods to get the image size in bytes only one gives the accurate results but that requires file to be saved in the disk. If I save the disk every time and read it again, it’ll take double the effort per iteration. IS there any way to read the image results accurately?

from PIL import Image
import os
import sys

image = Image.open(image_path
size_kb = os.stat(image_path).st_size
buffer = BytesIO()
image.save(buffer, format="jpeg", quality = 100, optimize = True) # Does not save but acts like an image saved to disc
size_kb2 = (buffer.getbuffer().nbytes)

printing the 3 different results print(size_kb, size_kb2, sys.getsizeof(image.tobytes()),) gives me 3 different results for the same image where os.stat gives accurate results (same results as shown by the Linux OS)

I do not want to save the image to disc to read it again because it’ll take a whole lot of time

whole Code:

STEP = 32
MIN_SIZE = 32

def resize_under_kb(image:Image,size_kb: float, desired_size:float)-> Image:
    '''
    Resize the image under given size in KB
    args:
        Image: Pil Image object
        size_kb: Current size of image in kb
        desired_size: Final desired size asked by user
    '''
    size = image.size
    new_width_height = max(size) - STEP # Decrease the pixels for first pass

    while new_width_height > MIN_SIZE and size_kb > desired_size: # either the image reaches minimun dimension possible or the desired possible size
        image = image.resize((new_width_height,new_width_height))  # keep on resizing until you get to desired output

        buffer = BytesIO()
        image.save(buffer, format="jpeg", quality = 100, optimize = True) # Does not save but acts like an image saved to disc
        size_kb = buffer.getbuffer().nbytes

        size = image.size # Current resized pixels
        new_width_height = max(size) - STEP # Dimensions for next iteration

    return image

Advertisement

Answer

This code:

size_kb = os.stat(image_path).st_size

prints the number of bytes an existing JPEG takes on disk.


This code:

buffer = BytesIO()
image.save(buffer, format="jpeg", quality = 100, optimize = True) # Does not save but acts like an image saved to disc
size_kb2 = (buffer.getbuffer().nbytes)

prints the number of bytes an image would take on disk if saved… by PIL’s current JPEG encoder, with its own Huffman tables and quality and chroma-subsampling and without allowing for file-system minimum block sizes.

This could be vastly different from the size you read from disk originally because that might have been created by different software, with different tradeoffs of speed and quality. It could even differ between two versions of PIL.


This code:

len(image.tobytes())

tells you the number of bytes your image is taking as currently decompressed in memory, without taking account of other data structures required for it and without taking account of metadata (comments, GPS data, copyright, manufacturer lens data and settings data).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement