當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python matplotlib.pyplot.imread()用法及代碼示例

Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。 Pyplot是Matplotlib模塊的基於狀態的接口,該模塊提供了MATLAB-like接口。

matplotlib.pyplot.imread()函數:

matplotlib庫的pyplot模塊中的imread()函數用於將文件中的圖像讀取到數組中。

用法: matplotlib.pyplot.imread(fname, format=None)


參數:此方法接受以下參數。

  • fname:此參數是要讀取的圖像文件。
  • format:此參數是假定用於讀取數據的圖像文件格式。

返回值:此方法返回以下內容。

  • imagedata:這將返回圖像數據

以下示例說明了matplotlib.pyplot中的matplotlib.pyplot.imread()函數:

範例1:

# Implementation of matplotlib function 
import numpy as np 
import matplotlib.cbook as cbook 
import matplotlib.image as image 
import matplotlib.pyplot as plt 
  
with cbook.get_sample_data('loggf.png') as image_file:
    image = plt.imread(image_file) 
  
fig, ax = plt.subplots() 
ax.imshow(image) 
ax.axis('off') 
  
plt.title('matplotlib.pyplot.imread() function Example',  
                                     fontweight ="bold") 
plt.show()

輸出:

範例2:

# Implementation of matplotlib function 
import numpy as np 
import matplotlib.cbook as cbook 
import matplotlib.image as image 
import matplotlib.pyplot as plt 
  
  
with cbook.get_sample_data('loggf.png') as file:
    im = image.imread(file) 
  
fig, ax = plt.subplots() 
  
ax.plot(np.cos(10 * np.linspace(0, 1)), '-o', ms = 15, 
                            alpha = 0.6, mfc ='green') 
  
ax.grid() 
fig.figimage(im, 10, 10, zorder = 3, alpha =.5) 
  
plt.title('matplotlib.pyplot.imread() function Example',  
                                     fontweight ="bold") 
plt.show()

輸出:




相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 matplotlib.pyplot.imread() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。