Matplotlib Imread: Illustration and Examples

Hello coders!! In this article, we will be learning about matplotlib imread. We will learn about its syntax and then see some illustrated examples. Without wasting any time, let us get straight into the topic.

matplotlib.pyplot.imread() Function:

This method is used to read an image from a file into an array.

Syntax Matplotlib Imread:

matplotlib.pyplot.imread(fname, format=None)

Parameters:

  • fname :  image file
  • format: image file format

Return Value of Matplotlib Imread:

This method returns the image data.

The image data. The returned array has shape

  • Grayscale images: (M, N)
  • RGB images: (M, N, 3)
  • RGBA images: (M, N, 4)

Illustrated Examples:

Let us now look into some examples to grasp a clearer concept.

Example 1: Loading Image:

import numpy as np 
import matplotlib.cbook as cbook 
import matplotlib.image as image 
import matplotlib.pyplot as plt 

with cbook.get_sample_data('img.jpg') as image_file: 
	image = plt.imread(image_file) 

fig, ax = plt.subplots() 
ax.imshow(image) 
ax.axis('off') 


plt.show() 
Loading Image
Output

cbook.get_sample_data() method is used to get the sample data file. Sample data files are stored in the ‘mpl-data/sample_data’ directory within the Matplotlib package. We then used the imread() method to return the image file. Lastly, using the imshow() method, we displayed the output.

Example 2: Watermark Image using matplotlib imread:

import numpy as np 
import matplotlib.cbook as cbook 
import matplotlib.image as image 
import matplotlib.pyplot as plt 


with cbook.get_sample_data('img.jpg') as file: 
	im = image.imread(file) 

fig, ax = plt.subplots() 

ax.plot(np.sin(10 * np.linspace(0, 1)), '-o', ms = 15,alpha = 0.6, mfc ='green') 


fig.figimage(im, 5, 5, zorder = 3, alpha =.5) 


plt.show() 

Watermark Image using matplotlib imread
Output

As you can see, in this example, we have used the same image as a background and plotted the sine function curve over it. We used the same steps as the previous example to load and display the image. We then plotted the sin method over it using the numpy sin method.

Example 3: Clipping Image With Patches:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.cbook as cbook


with cbook.get_sample_data('img.jpg') as image_file:
    image = plt.imread(image_file)

fig, ax = plt.subplots()
im = ax.imshow(image)
patch = patches.Circle((100, 100), radius=90, transform=ax.transData)
im.set_clip_path(patch)

ax.axis('off')
plt.show()
Clipping Image
Output

In this example, we have clipped our image. Just like the previous examples, we used the cbook.get_sample_data() method to load our image. We then used patches.Circle() method to create a circle at the center (100,100) and radius 90. Using the set_clip_path() method is used to set the artist’s clip-path.

Example 4: Matplotlib imread grayscale:

import matplotlib.pyplot as plt
import matplotlib.image as img

image = img.imread('img.jpg')

plt.imshow(image[:,:,1], cmap='gray')
plt.show()
Matplotlib imread
Output

For this code, we have converted the image into grayscale. Like the previous examples, we use the imread() method to read the image file. We then slice the array and use cmap gray to convert our image to grayscale.

Example 5:  Matplotlib imread RGB:

import matplotlib.pyplot as plt
import matplotlib.image as img
image = img.imread("img.jpg")
plt.imshow(image)
plt.show()
RGB
Output

Here, we have loaded the image using matplotlib imread in RGB format. We then used the imshow() method to display the loaded image.

Specify the type of image in matplotlib imread:

As discussed earlier, the syntax of imread is as follows:

matplotlib.pyplot.imread(fname, format=None)

The format parameter specifies the image file format that will be assumed for reading the data.  If not given, the format is deduced from the filename. If nothing can be deduced, PNG is tried.

Matplotlib imread vs cv2 imread:

Matplotlib imreadCV2 imread
Reads color value as RGBReads color value as BGR
Lists pixel rows from the bottom to the topLists pixel rows from the top to bottom
Less efficientMore efficient
matplotlib_img=img.imread('img.jpg')
plt.imshow(matplotlib_img)
cv_img=cv2.imread('img.jpg')
plt.imshow(cv_img)
matplotlib imreadcv2 imread

Also, Read

Conclusion:

With this, we come to an end with this article. I hope that the concept of matplotlib imread was clarified from this article. It is essentially useful when we want to implement an image into our graphical representation.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments