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?

JavaScript

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:

JavaScript

Advertisement

Answer

This code:

JavaScript

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


This code:

JavaScript

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:

JavaScript

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