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:
- PIL
- OpenCV
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
# importing the module import PIL from PIL import Image # loading the image img = PIL.Image. open ( "neveropen.png" ) # fetching the dimensions wid, hgt = img.size # displaying the dimensions 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
# importing the module import cv2 # loading the image img = cv2.imread( "neveropen.png" ) # fetching the dimensions wid = img.shape[ 1 ] hgt = img.shape[ 0 ] # displaying the dimensions print ( str (wid) + "x" + str (hgt)) |
Output:
600x135