Python Examples

Python Program to Find the Size (Resolution) of an Image

Source Code to Find Resolution of JPEG Image 

The Python image size program is used to find the size of a JPEG image.JPEG (Joint Photographic Experts Group) is one of the most common extensions. It is used as a compression technique for image compression. The files that are saved using the JPEG extension include a header that contains information about the file such as its name, size, resolution, last data the file was modified, etc. 

python image size

Source

The jpeg headers may also contain information about the dimensions of the image stored in the file. Along with it, it also stores the number or code of the colours the image contains as per the RGB or grayscale. 

The program for Python image size through with the resolution of a JPEG image can be found as provided below. You should be aware of file I/O and functions that are used in Python to understand and execute the program. 

The program will access the required information regarding the resolution of the image, or its dimensions, from the header of the JPEG file. It does not require an external source to extract this information from. 

                    

def res_image (filename):

              with open (filename, ‘rb’) as file_image:

                             file_image.seek (163)

                             a = file_image.read (2)

                             height = (a [0] << 8) + a [1]

                             a = file_image.read (2)

                             width = (a [0] << 8) + a [1]

              print (“The resolution of the given image is:”, width, “x”, height)

res_image (“image1.jpeg”)

The above program first accesses the required JPEG file and then converts it into binary form. Thus, it is opened in ‘rb’ form, that is, open the file in reading mode and in binary form. The height of the image is at the 164th position. The height and width of the JPEG image are stored in 2 bytes each. 

The code only works for those JPEG files that are available in JPEG File Interchange Format (JFIF). If the image is available in any other standard, then the code will not be able to calculate or access its resolution. 

The 2 bytes of the height and width of the image are then converted in number format using the bitwise operator. The << operator is known as the bitwise operator. 

To find the size of the image, one will need to add the following code along with the code that is used to find the dimensions of the JPEG file. It will provide the size, that is, the amount of storage used by the file in bytes. 

                    

import os 

def image_size (filename):

              sz = os.stat (filename)

              return sz.sz_size

Alternative Method

The PIL method can also be used to calculate the dimensions of a JPEG file. The code is much more efficient and requires a lesser number of lines of code. 

                    

from PIL import Image 

filepath = “image1.jpeg”

img = Image.open (filepath)

width, height = img.size

print (“The dimensions of the image are:”, width, “x”, height)

The above program will provide the same results as the previous one.

Frequently Asked Questions 

How do I get the size of an image in Python?

The Python image size can be obtained by importing the os module in the Python library and using the stat () method.  

How do you resize an image in Python GUI?

Using TKINTER, one can resize an image in Python GUI. You will need to use PIL to import ImageTK. 

How do you find the size of an image?

The size of the image is available in the header of the JPEG file. The size is recorded in the form of the number of bytes of memory that the file occupies.  

How many pixels is my image in Python?

The pixels in Python image size depend upon the values of R, G, B, and A. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.