(Last Updated On: 12/10/2023)
Python Program to Find the Size of Image Using PIL
# Python Program to Find the Size of Image Using PIL
#importing the module
import PIL
from PIL import Image
# loading the image
img = PIL.Image.open("img.png")
# fetching the dimensions
Width, height = img.size
# displaying the dimensions
print("the dimensions are :", str(width) + "x" + str(height))
Output:
the dimensions are :500x130
Python Program to Find the Size of Image Using OpenCV
# Python Program to Find the Size of Image Using OpenCV
# importing the module
import cv2
# loading the image
img = cv2.imread("geeksforgeeks.png")
# fetching the dimensions
w = img.shape[1]
h = img.shape[0]
# displaying the dimensions
print(“the dimensions are :”, str(w) + "x" + str(h))
Output:
the dimensions are :450x140
Python Program to Find the Size of Image
# Python Program to Find the Size of 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[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)
jpeg_res("img1.jpg")
Output:
The resolution of the image is 280 x 280