Introduction
A very basic Computer Vision operation is to read the visual data, display on the screen and also write or save visual data. The visual data is mostly like images and videos. In this tutorial we are going to see some OpenCV function which helps to read the visual input using cv2.imread(), display visual data on screen using cv2.imshow() and write the output file using cv2.imwrite().
Importing OpenCV Library
To begin this article, let us first import OpenCV library
#Lets import the OpenCV and Numpy library
import cv2
import numpy
Reading and Loading of an Image: cv2.imread()
To read an image ,opencv provide a cv2.imread() function . This function support various file format like BMP, PNG, JPEG, TIFF etc.
Syntax
Let us see the syntax
cv2.imread(path, flag)
Parameters:
path: The image should be in the working directory or a full path of image should be given.
flag: The flags option is used to control how the image is read.
cv2.IMREAD_COLOR: Loads a color image. Any transparency of the image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED: This reads all of the image data, including the alpha or transparency channel (if there is one) as a fourth channel.
Note: Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
If you are confused by seeing syntax of the function then don’t worry. We will take several examples to understand it completely.
[adrotate banner=”3″]
Display an image: cv2.imshow()
Use the function cv2.imshow() to display an image in a window.
Syntax
cv2.imshow(window_name, image)
window_name: A string representing the name of the window in which image to be displayed.
image: It is the image that is to be displayed.
Code to display an image
window_name=’image’
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds for any keyboard event. If you press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a keystroke.
cv2.destroyAllWindows() simply destroy all the windows we created. If you want to destroy any specific window, use the function cv2.destroyWindow() where you pass the exact window name as the argument.
Using cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) , we first create a window and load image to it later.
In cv2.namedWindow(),by default, the flag is cv2.WINDOW_AUTOSIZE. But if you specify flag to be cv2.WINDOW_NORMAL , you can resize the window. It will be helpful when an image is too large in dimension and adding track bar to windows.
Example1: Reading an image using cv2.imread() with flag cv2.IMREAD_COLOR
We will be using this sample image for our next two examples.

#Here I am providing only image name because this image is in my current
working directory.
#If this is not the case please provide the full path
img_first=cv2.imread('image.jpg',cv2.IMREAD_COLOR)
#you can also use below syntax for same
img=cv2.imread('image.jpg')
#or
img=cv2.imread('image.jpg',1)
Before going further let us explore an image
#Finding shape of an image
img_first.shape
(1227, 1534, 3)
#Finding the type of image
type(img_first)
numpy.ndarray
print(img_first)
[[[255 255 255] [255 255 255] [255 255 255] ... [255 255 255] [255 255 255] [255 255 255]] [[255 255 255] [255 255 255] [255 255 255] ... [255 255 255] [255 255 255] [255 255 255]] [[255 255 255] [255 255 255] [255 255 255] ... [255 255 255] [255 255 255] [255 255 255]] ... [[255 255 255] [255 255 255] [255 255 255] ... [255 255 255] [255 255 255] [255 255 255]] [[255 255 255] [255 255 255] [255 255 255] ... [255 255 255] [255 255 255] [255 255 255]] [[255 255 255] [255 255 255] [255 255 255] ... [255 255 255] [255 255 255] [255 255 255]]]
print(img_first.max())
print(img_first.min())
255 0
From the above three steps, we are able to know some important points regarding this image:
- An image is a multidimensional array(m n c), where m is the no. of rows, n is no. of columns and c is no. of channels.
- It has columns and rows of pixels, and each pixel has a value.
- The pixel values range from 0 to 255. That means 0 represents black and 255 represents white.
- The range is 0-255 means that each pixel is represented by a single 8-bit integer.
- Since the image is a colored image there are three channels. Opencv reads the image in Blue Green Red(BGR) format.
- So in the colored image, each pixel is represented by a three-element array, with each integer representing one of the three color channels: B, G, and R, respectively.
Display image using cv2.imshow()
window_name='imagefirst'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_first)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output Screenshot

Example2: Reading an image using cv2.imread() with flag cv2.IMREAD_GRAYSCALE
#Lets see second flag cv2.IMREAD_GRAYSCALE
img_second=cv2.imread('image.jpg',cv2.IMREAD_GRAYSCALE)
#or you can do like this
img_second=cv2.imread('image.jpg',0)
Let us explore this image
img_second.shape
(1227, 1534)
img_second.size
1882218
type(img_second)
numpy.ndarray
As you see here there are only rows and columns but there is no third channel. This is because in grayscale only one channel is there.
So we come to know some important points –
- An OpenCV image is a 2D or 3D array of the numpy.array type.
- An 8-bit grayscale image is a 2D array containing byte values. In an 8-bit grayscale image with a white pixel in the upper-left corner i.e image[0][0] is 255.
- A 24-bit BGR image is a 3D array, which also contains byte values.For a 24-bit (8-bit-per-channel) BGR image with a blue pixel in the upper-left corner i.e image[0][0][0] is [255, 0, 0].
Display this gray scale image
window_name='image_second'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_second)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output Screenshot

Example 3: Reading an image using cv2.imread() with flag cv2.IMREAD_UNCHANGED
A transparent image has four channels — 3 for color, and one for transparency. These images can be read in OpenCV using the cv2.IMREAD_UNCHANGED flag.
We are going to use the below image for this example.

img_third=cv2.imread('alph.png',cv2.IMREAD_UNCHANGED)
Let us explore this image and understand what transparency means
img_third.shape
(371, 602, 4)
As you see here, there are four channels in the image
With an image that has an alpha channel, each pixel has four components: red, blue, green, red and alpha. Alpha of 255 is opaque, while alpha of 0 is transparent.
Alpha channels are masks through which you can display images. The alpha channel is an 8-bit channel, which means it has 256 levels of gray from 0 (black) to 255 (white). White acts as the visible area; black acts as the transparent area (you see the background behind the image when displayed). The level of gray in between determines the level of visibility.
PNG, BMP and PSD file format actually support alpha channel.

Display this image
window_name='image_third'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_third)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output Screenshot

Saving or Writing image: cv2.imwrite()
Syntax
cv2.imwrite(filename, image)
filename: A string representing the file name. The filename must include image format like .jpg, .png, etc.
image: It is the image that is to be saved.
Example1: writing an image using cv2.imwrite() in current working directories
cv2.imwrite('imagefirst.jpg',img_first)
True
In this example, the image gets saved in current working directories.
If you want to save the image in specific directories then provide full path in filename parameter with image name like in below example
Example2: Writing an image using cv2.imwrite() in specific directories
cv2.imwrite('/home/sachin/Desktop/imagesecond.jpg',img_second)
True
Conclusion