Finding the Size Resolution of Image in Python
Let us see how to find the resolution of an image in Python. We will be solving this problem with two different libraries which are present in Python:
In our examples we will be using the following image:
The resolution of the above image is 600×135.
Using PIL
We will be using a library named Pillow to find the size(resolution) of the image. We will be using the function PIL.Image.open() to open and read our image and store the size in two variables by using the function img.size.
Python3
import PIL
from PIL import Image
img = PIL.Image. open ( "geeksforgeeks.png" )
wid, hgt = img.size
print ( str (wid) + "x" + str (hgt))
|
Output:
600x135
Using OpenCV
We will import OpenCV by importing the library cv2. We will load the image using the cv2.imread() function. After this, the dimensions can be found using the shape attribute. shape[0] will give us the height and shape[1] will give us the width.
Python3
import cv2
img = cv2.imread( "geeksforgeeks.png" )
wid = img.shape[ 1 ]
hgt = img.shape[ 0 ]
print ( str (wid) + "x" + str (hgt))
|
Output:
600x135
Last Updated :
03 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...