How to Find the Height and Width of an Image in Python
In this article, we show how to find the height and width of an image in Python.
Knowing the height and width is very important because there are so useful things that can be done with the width and height, such as calculating where to put certain things on the image or calculating its center.
To find the height and width of an image in Python, we use the following line, height, width= image.shape[:2]
where image is the image of the shape that you want to find the height and width for
You can then take these height and width variables and make calculations,
so as to determine the positioning of various things
Below is the full code of a program that finds the height and width
of an image and prints out the height and width values.
First we import the cv2 module because we use this module to read in the image.
We create a variable, image, that we read in an image through using the cv2.imread() function.
We then read in the height and width using the shape attribute.
We then print out the height and width values.
And this is how we can find the height and width of an image
in Python.
Related Resources
How to Draw a Rectangle in Python using OpenCV
How to Draw a Circle in Python using OpenCV
How to Draw a Line in Python using OpenCV
How to Add Text to an Image in Python using OpenCV
How to Display an OpenCV image in Python with Matplotlib
How to Use Callback functions to Connect Images to Events in Python using OpenCV
How to Check for Multiple Events in Python using OpenCV