詳解python中讀取和查看圖片的6種方法
本文主要介紹了python中讀取和查看圖片的6種方法,分享給大家,具體如下:
file_name1='test_imgs/spect/1.png' # 這是彩色圖片 file_name2='test_imgs/mri/1.png' # 這是灰度圖片
1 OpenCV
注:用cv2讀取圖片默認(rèn)通道順序是B、G、R
,而不是通常的RGB順序,所以讀進(jìn)去的彩色圖直接顯示會(huì)出現(xiàn)變色情況,詳情可以看:http://chabaoo.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讀進(jìn)來是三通道的圖片
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()
到此這篇關(guān)于詳解python中讀取和查看圖片的6種方法的文章就介紹到這了,更多相關(guān)python讀取和查看圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python常用庫Numpy進(jìn)行矩陣運(yùn)算詳解
這篇文章主要介紹了Python常用庫Numpy進(jìn)行矩陣運(yùn)算詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Python在內(nèi)網(wǎng)安裝python第三方包(庫)的方法
日常我們工作的計(jì)算機(jī)環(huán)境是內(nèi)網(wǎng),做項(xià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 計(jì)算方位角實(shí)例(根據(jù)兩點(diǎn)的坐標(biāo)計(jì)算)
今天小編就為大家分享一篇python 計(jì)算方位角實(shí)例(根據(jù)兩點(diǎn)的坐標(biāo)計(jì)算),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01keras 簡單 lstm實(shí)例(基于one-hot編碼)
這篇文章主要介紹了keras 簡單 lstm實(shí)例(基于one-hot編碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python數(shù)據(jù)結(jié)構(gòu)與算法之跳表詳解
跳表是帶有附加指針的鏈表,使用這些附加指針可以跳過一些中間結(jié)點(diǎn),用以快速完成查找、插入和刪除等操作。本節(jié)將詳細(xì)介紹跳表的相關(guān)概念及其具體實(shí)現(xiàn),需要的可以參考一下2022-02-02python 文件的基本操作 菜中菜功能的實(shí)例代碼
這篇文章主要介紹了python 文件的基本操作 菜中菜功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07Python基于更相減損術(shù)實(shí)現(xiàn)求解最大公約數(shù)的方法
這篇文章主要介紹了Python基于更相減損術(shù)實(shí)現(xiàn)求解最大公約數(shù)的方法,簡單說明了更相減損術(shù)的概念、原理并結(jié)合Python實(shí)例形式分析了基于更相減損術(shù)實(shí)現(xiàn)求解最大公約數(shù)的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-04-04