Image read and display image using python and openCV

Before Getting started you store your image on the same directory where your python is installed eg:C:\Python27 

Program: 

import numpy as np
import cv2 #import the open CV Lib

# Read the image from the disk (0 value depicts the read image ingray scale)
img=cv2.imread('colordog.jpg',0)

#display the img

cv2.imshow('image',img)

k=cv2.waitKey(0) #infinaately wait for the key to be pressed

if k==27: 
#wait for the esc key to be pressed
    cv2.destroyAllWindows()

elif k==ord('s'): # if the 's' key is pressed 
    cv2.imwrite('grayimag.jpg',img) 
#save gray img in disk 
    cv2.destroyAllWindows() 

# close all windows 


Download Source file

OUTPUT:




IF THERE IS ANY ERROR IN LOADING IMAGE YOU WILL GET AN ERROR IN THE CONSOLE AS SHOWN BELOW 



IF YOU HAVE ANY DOUBTS OR YOU NEED HELP IN YOUR PROJECT FEEL FREE TO MAIL : harimdg@gmail.com

Comments