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
sys.getsizeof. that's merely telling you the amount of RAM used by a specific objectnumber of pixels * memory taken by each bit? Just sayingquality=100is unlikely to be sensible if trying to reduce the size of an image! Please be clearer about your actual intention. What are you really trying to do with what type of images and what type of pixel dimensions and what type of sizes in bytes?