JPEG stands for Joint Photographic Experts Group, and is one of the most widely used compression techniques for image compression. Most of the image file formats have headers stored in the initial few bytes that retain some useful information about the file.
In the following program, we will find out the resolution of the JPEG image by reading the information stored in the header.
The program below will only run for JPEG images, as every file format uses a slightly different way to store the same information.
We are going to find the resolution of this image.
# Program to find the resolution of an imagedef find_res(filename):with open(filename,'rb') as img_file: # open image in binary mode# height of image is at 164th positionimg_file.seek(163)# read the two bytesa = img_file.read(2)# calculate heightheight = (a[0] << 8) + a[1]# read next two bytes which stores the widtha = img_file.read(2)# calculate widthwidth = (a[0] << 8 ) + a[1]print("IMAGE RESOLUTION IS : ",width,"X",height)find_res("sample_image.jpg")