Python Program to Find the Size (Resolution) of a Image: You will learn to find resolution of a jpeg image in this example without using external libraries.
PEG (pronounced “jay-peg”) stands for Joint Photographic Experts Group. It is one of the most widely used compression techniques for image compression.
Python Program to Find the Size Resolution of a Image
def jpeg_res(filename): """"This function prints the resolution of the jpeg image file passed into it""" # open image for reading in binary mode with open(filename,'rb') as img_file: # height of image (in 2 bytes) is at 164th position img_file.seek(163) # read the 2 bytes a = img_file.read(2) # calculate height height = (a << 8) + a # next 2 bytes is width a = img_file.read(2) # calculate width width = (a << 8) + a print("The resolution of the image is",width,"x",height) jpeg_res("img1.jpg")