1import cv2
2img = cv2.imread('/path_to_image/opencv-logo.png')
3cv2.imshow('image',img)
4cv2.waitKey(0)
5cv2.destroyAllWindows()
6
7# to use it in a loop
8k = cv2.waitKey(0)
9if k == 27: # wait for ESC key to exit
10 cv2.destroyAllWindows()
11elif k == ord('s'): # wait for 's' key to save and exit
12 cv2.imwrite('messigray.png',img)
13 cv2.destroyAllWindows()
14
1 import numpy as np
2 import cv2
3 import matplotlib.pyplot as plt
4 %matplotlib inline # if you are running this code in jupyter notebook
5
6 img = cv2.imread('/path_to_image/opencv-logo.png',0) # reads image 'opencv-logo.png' as grayscale
7 plt.imshow(img, cmap='gray')