# load and display an image import matplotlib.pyplot as plt img = plt.imread("mandrill.jpg") size = img.shape plt.figure(figsize=(size[0]/72,size[1]/72)) # figure size is specified in inches, specify as pixels/pixels_per_inch plt.imshow(img) # display the image #plt.axis('off') print( size ) # each pixel of an RGB image consists of three R = img[:,:,0] G = img[:,:,1] B = img[:,:,2] plt.subplot(131); plt.imshow(R); plt.axis('off'); plt.subplot(132); plt.imshow(G); plt.axis('off'); plt.subplot(133); plt.imshow(B); plt.axis('off'); # specify the colormap (mapping from value to color) when displaying grayscale images R = img[:,:,0] G = img[:,:,1] B = img[:,:,2] plt.subplot(131); plt.imshow(R, cmap='gray'); plt.axis('off'); plt.title( 'red' ) plt.subplot(132); plt.imshow(G, cmap='gray'); plt.axis('off'); plt.title( 'green' ) plt.subplot(133); plt.imshow(B, cmap='gray'); plt.axis('off'); plt.title( 'blue' ) # convert from RGB to gray and explore colormaps gray = 0.2989*R + 0.5870*G + 0.1140*B size = gray.shape print(size) plt.figure(figsize=(size[0]/72,size[1]/72)) # figure size is specified in inches, specify as pixels/pixels_per_inch plt.imshow(gray,cmap='jet') # display the image #plt.imshow(gray,cmap='rainbow') #plt.imshow(gray,cmap='ocean') #plt.imshow(gray,cmap='inferno') plt.axis('off') plt.colorbar() # colormap limits plt.subplot(121); plt.imshow(gray, cmap='jet') plt.colorbar(ticks=[1, 100, 200], orientation='horizontal') plt.axis('off'); plt.subplot(122); h = plt.imshow(gray, cmap='jet') h.set_clim(50, 150) plt.colorbar(ticks=[50, 100, 150], orientation='horizontal') plt.axis('off'); # histogram of intensity values (ravel converts 2-D to 1-D) plt.hist(gray.ravel(), bins=256, range=(0.0, 255) ); R = img[:,:,0] G = img[:,:,1] B = img[:,:,2] plt.figure(figsize=(10,5)) plt.subplot(131); plt.hist(R.ravel(), bins=256, range=(0.0, 255) ); plt.title( 'red' ); plt.subplot(132); plt.hist(G.ravel(), bins=256, range=(0.0, 255) ); plt.title( 'green' ); plt.subplot(133); plt.hist(B.ravel(), bins=256, range=(0.0, 255) ); plt.title( 'blue' );