詳解python中讀取和查看圖片的6種方法
更新時間:2022年04月19日 10:55:39 作者:大閘謝Gemini
本文主要介紹了詳解python中讀取和查看圖片的6種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
本文主要介紹了python中讀取和查看圖片的6種方法,分享給大家,具體如下:
file_name1='test_imgs/spect/1.png' # 這是彩色圖片 file_name2='test_imgs/mri/1.png' # 這是灰度圖片
1 OpenCV
注:用cv2讀取圖片默認通道順序是B、G、R
,而不是通常的RGB順序,所以讀進去的彩色圖直接顯示會出現(xiàn)變色情況,詳情可以看:http://www.dbjr.com.cn/article/245048.htm
import cv2 spect= cv2.imread(file_name1) # BGR spect= spect[:, :, ::-1] # RGB mri= cv2.imread(file_name2) # 灰度圖 print(spect.shape) # (256, 256, 3) print(mri.shape) # (256, 256, 3) cv2讀進來是三通道的圖片
import matplotlib.pyplot as plt plt.imshow(spect) plt.show()
import matplotlib.pyplot as plt fig=plt.figure() f1 = fig.add_subplot(121) f2 = fig.add_subplot(122) f1.imshow(spect) f2.imshow(mri) plt.show()
2 imageio
import imageio spect = imageio.imread(file_name1) mri = imageio.imread(file_name2) print(spect.shape) # (256, 256, 3) print(mri.shape) # (256, 256)
import matplotlib.pyplot as plt fig=plt.figure() f1 = fig.add_subplot(121) f2 = fig.add_subplot(122) f1.imshow(spect) f2.imshow(mri,cmap='gray') # 注:單通道灰度圖必須加上cmap='gray'才能正確顯示 plt.show()
3 PIL
from PIL import Image import numpy as np spect= Image.open(file_name1) # <PIL.PngImagePlugin.PngImageFile image mode=RGB size=256x256 at 0x1D9F15FFDC8> spect.show()
4 scipy.misc
from scipy.misc import imread spect = imread(file_name1) mri = imread(file_name2)
import matplotlib.pyplot as plt fig=plt.figure() f1 = fig.add_subplot(121) f2 = fig.add_subplot(122) f1.imshow(spect) f2.imshow(mri,cmap='gray') # 注:單通道灰度圖必須加上cmap='gray'才能正確顯示 plt.show()
5 tensorflow
from tensorflow.python.keras.preprocessing.image import load_img spect = load_img(file_name1) # <PIL.PngImagePlugin.PngImageFile image mode=RGB size=256x256 at 0x1D9EF188048>
spect.show()
6 skimage
from skimage import io import matplotlib.pyplot as plt mri = io.imread(file_name2)#讀取數(shù)據(jù) plt.imshow(mri,cmap='gray') # 注:單通道灰度圖必須加上cmap='gray'才能正確顯示 plt.show()
到此這篇關于詳解python中讀取和查看圖片的6種方法的文章就介紹到這了,更多相關python讀取和查看圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python在內(nèi)網(wǎng)安裝python第三方包(庫)的方法
日常我們工作的計算機環(huán)境是內(nèi)網(wǎng),做項目需要安裝python?第三方庫,在外網(wǎng)一般使用"pip?install?包名"很快就安裝上了,但是在內(nèi)網(wǎng)無法連接pypi網(wǎng)站或者國內(nèi)鏡像,那該如何安裝呢,下面小編給大家?guī)砹薖ython如何在內(nèi)網(wǎng)安裝python第三方包庫,感興趣的朋友一起看看吧2024-01-01Python基于更相減損術實現(xiàn)求解最大公約數(shù)的方法
這篇文章主要介紹了Python基于更相減損術實現(xiàn)求解最大公約數(shù)的方法,簡單說明了更相減損術的概念、原理并結合Python實例形式分析了基于更相減損術實現(xiàn)求解最大公約數(shù)的相關操作技巧與注意事項,需要的朋友可以參考下2018-04-04