• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

317
Views
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)
almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error