YouTip LogoYouTip

Matplotlib Imread

imread() is a function in the Matplotlib library used to read image data from an image file. The imread() method returns a numpy.ndarray object with the shape (nrows, ncols, nchannels), representing the number of rows, columns, and channels of the read image: * If the image is a grayscale image, nchannels is 1. * If the image is a color image, nchannels is 3 or 4, representing the three color channels (red, green, blue) and an alpha channel respectively. The syntax of imread() method is as follows: matplotlib.pyplot.imread(fname, format=None) **Parameter Description:** * `fname`: Specifies the file name or file path of the image file to read, which can be a relative path or an absolute path. * `format`: Specifies the format of the image file. If not specified, the format is automatically identified based on the file extension. The following example demonstrates how to use the imread function to read image data from an image file and display it: ## Example import matplotlib.pyplot as plt # Read Image File, Download URL: https://static.jyshare.com/images/demo/map.jpeg img = plt.imread('map.jpeg') # Display Image plt.imshow(img) plt.show() In the above example, we first used the imread() method to read image data from an image file named map.jpeg and stored it in the variable img. Then we used the imshow() method to display this image. Note: We did not specify a colormap when displaying the image because the imread() method has already converted the image data to RGB format with the correct colormap, so we can directly use the default colormap to display the image. The result is as follows: !(#) We can modify the image by changing the numpy array. For example, if we multiply the array by a number 0≀x≀1, we darken the image: ## Example import matplotlib.pyplot as plt # Read Image File, Download URL: https://static.jyshare.com/images/mix/tiger.jpeg img_array = plt.imread('tiger.jpeg') tiger = img_array/255 #print(tiger) # Display Image plt.figure(figsize=(10,6)) for i in range(1,5): plt.subplot(2,2,i) x =1 - 0.2*(i-1) plt.axis('off')#hide coordinate axes plt.title('x={:.1f}'.format(x)) plt.imshow(tiger*x) plt.show() The result is as follows: !(#) The following example demonstrates how to crop an image: ## Example import matplotlib.pyplot as plt # Read Image File, Download URL: https://static.jyshare.com/images/mix/tiger.jpeg img_array = plt.imread('tiger.jpeg') tiger = img_array/255 #print(tiger) # Display Image plt.figure(figsize=(6,6)) plt.imshow(tiger[:300,100:400,:]) plt.axis('off') plt.show() The result is as follows: !(#) If we set the array elements of the green and blue coordinates of the RGB color to 0, we will get a red image: ## Example import matplotlib.pyplot as plt # Read Image File, Download URL: https://static.jyshare.com/images/mix/tiger.jpeg img_array = plt.imread('tiger.jpeg') tiger = img_array/255 #print(tiger) # Display Image red_tiger = tiger.copy() red_tiger[:, :,[1,2]]=0 plt.figure(figsize=(10,10)) plt.imshow(red_tiger) plt.axis('off') plt.show() The result is as follows: !(#)
← Vue3 SfcMatplotlib Imshow β†’