Python cv2 Image Size: How to Get Image Size in Python

Python cv2 Image Size: How to Get Image Size in Python

To get the proper size of an image, use numpy.shape property. In OpenCV, the image size (width, height) can be obtained as a tuple.

Python cv2 module uses the numpy library to manipulate the images. Here one thing to note that I am assuming that you are working with BGR images.

Python cv2 Image Size

To get the proper size of an image, use  numpy.shape property. In OpenCV, we can get the image size (width, height) as a tuple with the attribute shape of ndarray.

To get the image size (width, height) with OpenCV, use the ndarray.shape. It works on the following kind of image.

  1. For color image
  2. For grayscale (monochrome) images

For color Image

Let’s talk about the color image. In the case of a color image, it is a 3D ndarray of row (height) x column (width) x color (3). The shape is a tuple of (row (height), column (width), color (3)).

## Importing cv2
import cv2

## Path
img = cv2.imread('data.jpg')

## <class 'numpy.ndarray'>
print(img.shape)

python python cv2 opencv

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python cv2 rectangle: Draw a Rectangle using OpenCV

To draw a rectangle in Python using OpenCV, use cv2.rectangle() function. The cv2 rectangle() method is used to draw a rectangle on any image.

Create a Virtual Pen and Eraser with Python OpenCV - Genial Code

Learn Free how to create a virtual pen and eraser with python and OpenCV with source code and complete guide. This entire application is built fundamentally on contour detection. It can be thought of as something like closed color curves on compromises that have the same color or intensity, it's like a blob. In this project we use color masking to get the binary mask of our target color pen, then we use the counter detection to find the location of this pen and the contour to find it.

Python cv2: How to Filter Image Pixels using OpenCV

To filter image pixels means you can convert the image from color to grayscale or add an extra layer to the image.

Python cv2: Filtering Image using GaussianBlur() Method

There are three filters available in the OpenCV-Python library. Gaussian Blur Filter, Erosion Blur Filter, Dilation Blur Filter.

Python cv2 erode: How to Erode Images using OpenCV

There are two main morphological transformations.1) Image Dilate 2) Image Erosion. The cv2 module that supports the following two 1) cv2.dilate() 2) cv2.erode().