opencv Basic reading and display of an image


import cv2

image_path= #put your image path here

#use imread() function to read image data to variable img.
img = cv2.imread(image_path) 

#display image data in a new window with title 'I am an image display window'
cv2.imshow('I am an image display window',img) 

#wait until user hits any key on keyboard

#close any windows opened by opencv

To control the size of the display window on the screen, add the following commands before the cv2.imshow command:

window_width=800 #size of the display window on the screen

#open an empty window with a title.
#The flag cv2.WINDOW_NORMAL allows the window to be scaleable.
cv2.namedWindow('I am an image display window', cv2.WINDOW_NORMAL)

#scale the image display window to desired size
cv2.resizeWindow('I am an image display window', window_width, window_height)

see openCV docs for further details