Import Libraries


In [1]:
import cv2

In [2]:
import matplotlib.pyplot as plt
%matplotlib inline

Read image and Check size and resolution of image


In [3]:
img = cv2.imread('demo1.jpg')

In [4]:
plt.imshow(img)


Out[4]:
<matplotlib.image.AxesImage at 0x7f796cd4bc18>
OpenCV represents RGB images as multi-dimensional NumPy arrays…but in reverse order! This means that images are actually represented in BGR order rather than RGB!

In [5]:
print(img.shape)


(1080, 1920, 3)

In [6]:
print('Filesize:', img.size/1024/8, 'KB')


Filesize: 759.375 KB

In [7]:
print('Type:', img.dtype)


Type: uint8

To GrayScale


In [8]:
img_gray = cv2.imread('demo1.jpg', cv2.IMREAD_GRAYSCALE)

In [9]:
plt.imshow(img_gray, cmap='gray')


Out[9]:
<matplotlib.image.AxesImage at 0x7f796cc3bdd8>

In [10]:
print('Shape_gray:', img_gray.shape)


Shape_gray: (1080, 1920)

In [11]:
print('File_size_gray:', img_gray.size/1024/8, 'KB')


File_size_gray: 253.125 KB

In [12]:
print('Type_gray:', img_gray.dtype)


Type_gray: uint8
Since in gray scale only one color is there, so no depth and therefore img_gray.shape will have only 2 paprameters in it.

To RGB color


In [13]:
img_RGB = cv2.imread('demo1.jpg')

In [14]:
plt.imshow(cv2.cvtColor(img_RGB, cv2.COLOR_BGR2RGB))


Out[14]:
<matplotlib.image.AxesImage at 0x7f796cbe0358>

In [15]:
print('Shape_RGB:', img_RGB.shape)


Shape_RGB: (1080, 1920, 3)

In [16]:
print('File_size_RGB:', img_RGB.size/1024/8, 'KB')


File_size_RGB: 759.375 KB

In [17]:
print('Type_RGB:', img_RGB.dtype)


Type_RGB: uint8