'How to use img.shape() in javascript

import cv2

read image

img = cv2.imread('/home/img/python.png', cv2.IMREAD_UNCHANGED)

get dimensions of image

dimensions = img.shape

height, width, number of channels in image

height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]

 
print('Image Dimension    : ',dimensions)
print('Image Height       : ',height)
print('Image Width        : ',width)
print('Number of Channels : ',channels)


Solution 1:[1]

for the amount of channels you need to do

channels = image.shape[-1] if image.ndim == 3 else 1

This is because the amount of channels will not be shown if it is grayscale. And that is why image.ndim is checked.

To get width and height just do what you already did in your example

And for dimensions you could do:

dimensions = f"{width}x{height}"

if you want it to return in a string. Otherwise, if you want it to return as a tuple it will just be the first 2 objects in img.shape.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 maxim pavlenko