# Typical importsimport cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
# Tells notebook to render figures in-page.%matplotlib inline
from IPython.display import Image
# Display Images Directly in the BrowserImage(filename='img_bw_18x18.png')
# Read Images using OpenCVretval = cv2.imread(filename[, flags])
# Print the size of image.print('Image size is ', bw_img.shape)
# Print data-type of image.print('Data type of image is ', bw_img.dtype)
# Display the image.plt.imshow(bw_img);
# Set color map to gray scale for proper rendering.plt.imshow(bw_img, cmap ='gray');
# Save BGR filecv2.imwrite(filename, img[, params])
Note: To avoid specifying a specific color map each time you call imshow() you can include the line below at the top of your notebook where the import statements are located which will set the color map globally.
matplotlib.rc('image', cmap = 'gray')
### Read and display an imageimage = cv2.imread('Apollo-8-Launch.jpg')
plt.figure(figsize = [10, 10])
plt.imshow(image)
plt.title('Apollo-8-Launch.jpg', fontsize =16);
# Save the image to the file system.# The file format is determined by the file extension.cv2.imwrite('Apollo-8-Launch.png', image)