In this tutorial, we will discuss how to get memory size of an image by using a Python program. It is very easy to find the memory size of an image by using Python code.
- Prerequisite: Install pillow library, to install run the following command:
pip install pillow
- Import the Image PIL(Python Imaging Library)import module we can solve the program.
from PIL import Image
- PIL.Image.open(): Open and identifies the given image file. This is a lazy operation; this function identifies the files, but the file remains open, and actual image data is not read from the file until you try to process the data(or call the load() method).
image=Image.open("https://www.codespeedy.com/wp-content/themes/CodeSpeedy-March-2019/img/logo/CodeSpeedy-Logo.png")
- To get the memory size of an image proved this arguments width,height,getbands of an image and assign them to variables.
rotating=image.width * image.height * len(image.getbands())
- print the statements
print(rotating)
- Final code will be like this:
from PIL import Image image=Image.open("https://www.codespeedy.com/wp-content/themes/CodeSpeedy-March-2019/img/logo/CodeSpeedy-Logo.png") rotating=image.width * image.height * len(image.getbands()) point=rotating/1024 print("{ :.2f} KB".format(point))
Final Output:
32.02 KB