OpenCV Python - Get Image Shape/Dimensions
https://www.tutorialkart.com/opencv/python/opencv-python-get-image-size/
In Image Processing applications, it is often necessary to know the size of an image that is loaded or transformed through various stages. In this OpenCV Tutorial, we will learn how to get image size in OpenCV Python with an example.
When working with OpenCV Python, images are stored in numpy ndarray
. To get the image shape or size, use ndarray.shape
to get the dimensions of the image. Then, you can use index on the dimensions variable to get width, height and number of channels for each pixel.
In the following code snippet, we have read an image to img ndarray
. And then we used ndarray.shape
to get the dimensions of the image.
1. Example Python Script - OpenCV Get Image Size
In this example, we have read an image and used ndarray.shape
to get the dimension.
We can access height, width and number of channels from img.shape: Height is at index 0, Width is at index 1; and number of channels at index 2.
snippet [ˈsnɪpɪt]:n. 小片,片断,不知天高地厚的年轻人
img.shape
returns (Height, Width, Number of Channels)
- Height represents the number of pixel rows in the image or the number of pixels in each column of the image array. (Height 表示图像中的像素行数或图像阵列每一列中的像素数。)
- Width represents the number of pixel columns in the image or the number of pixels in each row of the image array. (Width 表示图像中的像素列数或图像阵列每一行中的像素数。)
- Number of Channels represents the number of components used to represent each pixel.
# height, width, number of channels in image
# height = image.shape[0], width = image.shape[1], channels = image.shape[2]
component [kəmˈpəʊnənt]:n. 组成部分,成分,组件,元件 adj. 组成的,构成的
transparency [trænsˈpærənsi]:n. 透明,透明度,幻灯片,有图案的玻璃
neglect [nɪˈɡlekt]:v. 忽视,忽略,疏忽,漏做 n. 忽略,忽视,未被重视
Second argument is a flag which specifies the way image should be read.
cv2.IMREAD_COLOR: Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE: Loads image in grayscale mode
cv2.IMREAD_UNCHANGED: Loads image as such including alpha channel. In the above example, Number of Channels = 4 represent Alpha, Red, Green and Blue channels
.
Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
1.1 cv2.IMREAD_COLOR
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))
import numpy as np
# import tensorflow as tf
import cv2
# import time
def inference(image_file, current_directory):
img = cv2.imread(image_file, cv2.IMREAD_COLOR)
# 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)
tmp_directory = current_directory + "/tmp"
if not os.path.exists(tmp_directory):
os.makedirs(tmp_directory)
cv2.namedWindow("Press ESC on keyboard to exit.", cv2.WINDOW_NORMAL)
# Display the resulting frame
cv2.imshow("Press ESC on keyboard to exit.", img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
pass
elif k == ord('s'): # wait for 's' key to save and exit
image_name = "%s/%s.jpg" % (tmp_directory, "source_image")
cv2.imwrite(image_name, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
# When everything done, release the capture
cv2.destroyAllWindows()
if __name__ == '__main__':
image_file = "./tmp/000505.jpg"
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
print("os.environ['CUDA_VISIBLE_DEVICES']:", os.environ['CUDA_VISIBLE_DEVICES'])
inference(image_file, current_directory)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
os.environ['CUDA_VISIBLE_DEVICES']: 0
Image Dimension : (1080, 1920, 3)
Image Height : 1080
Image Width : 1920
Number of Channels : 3
Process finished with exit code 0
1.2 cv2.IMREAD_GRAYSCALE
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))
import numpy as np
# import tensorflow as tf
import cv2
# import time
def inference(image_file, current_directory):
img = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)
# 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)
tmp_directory = current_directory + "/tmp"
if not os.path.exists(tmp_directory):
os.makedirs(tmp_directory)
cv2.namedWindow("Press ESC on keyboard to exit.", cv2.WINDOW_NORMAL)
# Display the resulting frame
cv2.imshow("Press ESC on keyboard to exit.", img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
pass
elif k == ord('s'): # wait for 's' key to save and exit
image_name = "%s/%s.jpg" % (tmp_directory, "source_image")
cv2.imwrite(image_name, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
# When everything done, release the capture
cv2.destroyAllWindows()
if __name__ == '__main__':
image_file = "./tmp/000505.jpg"
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
print("os.environ['CUDA_VISIBLE_DEVICES']:", os.environ['CUDA_VISIBLE_DEVICES'])
inference(image_file, current_directory)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
os.environ['CUDA_VISIBLE_DEVICES']: 0
Image Dimension : (1080, 1920)
Image Height : 1080
Image Width : 1920
Process finished with exit code 0
1.3 cv2.IMREAD_UNCHANGED
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))
import numpy as np
# import tensorflow as tf
import cv2
# import time
def inference(image_file, current_directory):
img = cv2.imread(image_file, 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)
tmp_directory = current_directory + "/tmp"
if not os.path.exists(tmp_directory):
os.makedirs(tmp_directory)
cv2.namedWindow("Press ESC on keyboard to exit.", cv2.WINDOW_NORMAL)
# Display the resulting frame
cv2.imshow("Press ESC on keyboard to exit.", img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
pass
elif k == ord('s'): # wait for 's' key to save and exit
image_name = "%s/%s.jpg" % (tmp_directory, "source_image")
cv2.imwrite(image_name, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
# When everything done, release the capture
cv2.destroyAllWindows()
if __name__ == '__main__':
image_file = "./tmp/000505.jpg"
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
print("os.environ['CUDA_VISIBLE_DEVICES']:", os.environ['CUDA_VISIBLE_DEVICES'])
inference(image_file, current_directory)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
os.environ['CUDA_VISIBLE_DEVICES']: 0
Image Dimension : (1080, 1920, 3)
Image Height : 1080
Image Width : 1920
Number of Channels : 3
Process finished with exit code 0