Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

jpeg
python3
communitycreator

How to find the resolution of the image using python

222010301037_vaibhavnirnai

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.

Code

We are going to find the resolution of this image.

widget
# Program to find the resolution of an image 
def find_res(filename):
    with open(filename,'rb') as img_file: # open image in binary mode
        # height of image is at 164th position
        img_file.seek(163)
        # read the two bytes 
        a = img_file.read(2)
        # calculate height
        height = (a[0] << 8) + a[1]
        # read next two bytes which stores the width
        a = img_file.read(2)
        # calculate width
        width = (a[0] << 8 ) + a[1]
    print("IMAGE RESOLUTION IS : ",width,"X",height)
find_res("sample_image.jpg")

RELATED TAGS

jpeg
python3
communitycreator

CONTRIBUTOR

222010301037_vaibhavnirnai
RELATED COURSES

View all Courses

Keep Exploring