python program to find the size (resolution) of image

This is a python program to find the size of an image




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# python program to find the size (resolution) of image
# python program to find the resolution
# of a jpeg without using
# any external libraries

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[0]<<8) + a[1]
        # next 2 bytes is width
        a = img_file.read(2)
        # calculate width
        width = (a[0]<<8) + a[1]
    print("the resolution of the image is",width,"x",height)

filename = input("Enter the file name followed by the extention ('image.jpg'): ")
jpeg_res(filename)

Output:

 Enter the file name: image.jpg
the resolution of the image is 5162 x 2827

Comments