read-image-using-opencv-python

Read Image using OpenCV in Python | OpenCV Tutorial | Computer Vision

Reading image is first step to perform any operation on raw image in OpenCV and Computer Vision. Now we are going to learn How to read image in OpenCV using cv2.imread() function in detail.

read-image-using-opencv-python

Read Image using OpenCV Python

Syntax: cv2.imread(path, flag)

flag: default value is cv2.IMREAD_COLOR

Parameters:

cv2.IMREAD_COLOR or 1: reads the image with RGB colors

cv2.IMREAD_GRAYSCALE or 0: reads the image with gray colors

cv2.IMREAD_UNCHANGED or -1: It reads the image as is from the source. If the source image is an RGB, it loads the image into an array with Red, Green, and Blue channels. If the source image is ARGB, it loads the image with three color components along with the alpha or transparency channel.

CODE:

Read Image in RGB format

import cv2 #import opencv packages

path = "adult_blur_boardwalk.jpg" 

img = cv2.imread(path, 1) #read image in RGB format

print(img.shape)#print shape of image
print(img) #print image array

cv2.imshow('image', img) #show image
cv2.waitKey(0) #wait to close image window

cv2.destroyAllWindows() #cloase all windows

Read Image in Grey Format

import cv2 #import opencv packages

path = "adult_blur_boardwalk.jpg" 

img = cv2.imread(path, 0) #read image in grey format

print(img.shape)#print shape of image
print(img) #print image array

cv2.imshow('image', img) #show image
cv2.waitKey(0) #wait to close image window

cv2.destroyAllWindows() #cloase all windows

Read Image in Alpha or Transference Format

import cv2 #import opencv packages

path = "adult_blur_boardwalk.jpg" 

img = cv2.imread(path, -1) #read image alpha or transparency format

print(img.shape)#print shape of image
print(img) #print image array

cv2.imshow('image', img) #show image
cv2.waitKey(0) #wait to close image window

cv2.destroyAllWindows() #cloase all windows

Read Image from Full Image Path

import cv2

path = r"""C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"""

img = cv2.imread(path, 1) #read image from full image path

print(img.shape)
print(img)

cv2.imshow('image', img)
cv2.waitKey(0)

cv2.destroyAllWindows()

Leave a Reply