溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中如何讀取圖片

發(fā)布時間:2021-07-05 18:02:18 來源:億速云 閱讀:481 作者:Leah 欄目:編程語言

Python中如何讀取圖片,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

一、OpenCV讀取圖片

OpenCV讀取的圖片,直接就是numpy.ndarray格式,無需轉(zhuǎn)換

import cv2 img_cv   = cv2.imread(dirpath)#讀取數(shù)據(jù) print("img_cv:",img_cv.shape) img_cv: (1856, 2736, 3) print("img_cv:",type(img_cv)) img_cv: <class 'numpy.ndarray'> #看下讀取的數(shù)據(jù)怎么樣 img_cv array([[[  0,   3,   0],         [ 11,  20,  17],         ...,         [  5,  23,  16]],        [[  0,   2,   0],         ...,         [  5,  23,  16]]]

二、PIL讀取圖片

PIL讀取的圖片并不是直接的numpy.ndarray格式,需要進行轉(zhuǎn)換

from PIL import Image import numpy as np  img_PIL = Image.open(dirpath)#讀取數(shù)據(jù)  print("img_PIL:",img_PIL) img_PIL: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=2736x1856 at 0x2202A8FC108>  print("img_PIL:",type(img_PIL)) img_PIL: <class 'PIL.JpegImagePlugin.JpegImageFile'>  #將圖片轉(zhuǎn)換成np.ndarray格式 img_PIL = np.array(img_PIL) print("img_PIL:",img_PIL.shape) img_PIL: (1856, 2736, 3) print("img_PIL:",type(img_PIL)) img_PIL: <class 'numpy.ndarray

三、keras讀取圖片

keras深度學習的框架,里面也是內(nèi)置了讀取圖片的模塊,該模塊讀取的也不是數(shù)組格式,需要進行轉(zhuǎn)換。

from keras.preprocessing.image import array_to_img, img_to_array  load_imgload_imgimg_keras = load_img(dirpath)#讀取數(shù)據(jù)  print("img_keras:",img_keras) img_keras: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=2736x1856 at 0x2201D184BC8>  print("img_keras:",type(img_keras)) img_keras: <class 'PIL.JpegImagePlugin.JpegImageFile'>  #使用keras里的img_to_array() img_keras = img_to_array(img_keras) print("img_keras:",img_keras.shape) img_keras: (1856, 2736, 3) print("img_keras:",type(img_keras)) img_keras: <class 'numpy.ndarray'>  #可以使用使用np.array()進行轉(zhuǎn)換 mg_keras= np.array(img_keras)

四、skimage讀取圖片

scikit-image是基于scipy的一款圖像處理包,它將圖片作為numpy數(shù)組進行處理,讀取的數(shù)據(jù)正好是numpy.ndarray格式。

import skimage.io as io img_io = io.imread(dirpath)#讀取數(shù)據(jù)  print("img_io :",img_io .shape) img_io : (1856, 2736, 3)  print("img_io :",type(img_io )) img_io : <class 'numpy.ndarray'

五、matplotlib.image讀取圖片

利用matplotlib.image讀取的圖片,直接就生成了數(shù)組格式

import matplotlib.image as mpig

img_mpig = mpig.imread(dirpath)#讀取數(shù)據(jù)  print("img_mpig :",img_mpig .shape) img_mpig : (1856, 2736, 3)  print("img_mpig :",type(img_mpig )) img_mpig : <class 'numpy.ndarray'

六、matplotlib.pyplot讀取圖片

利用matplotlib.pyplot讀取的圖片,同樣也是直接就生成了數(shù)組格式

import matplotlib.pyplot as plt img_plt = plt.imread(dirpath) print("img_plt :",img_plt .shape) img5: (1856, 2736, 3) print("img_plt :",type(img_plt )) img5: <class 'numpy.ndarray'>

七、顯示讀取的圖片

同樣,使用matplotlib 包可以打印出來讀取的照片,要打印上述案例中讀取的照片,只需要下面兩行代碼就行了。

plt.imshow(img_plt , cmap=plt.cm.binary) plt.show()

Python中如何讀取圖片

圖片三通道的,打印其中一個通道

plt.imshow(img_plt[:,:,1] , cmap=plt.cm.binary) plt.show()

Python中如何讀取圖片

當然,我們可以隨便構(gòu)造一個數(shù)組,可以顯示出來

digit  =  [[135,26,33,12],[14,27,43,190],[120,124,134,205]] plt.imshow(digit, cmap=plt.cm.binary) plt.show()

Python中如何讀取圖片

看完上述內(nèi)容,你們掌握Python中如何讀取圖片的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI