詳解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://www.dhdzp.com/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') # 注:?jiǎn)瓮ǖ阑叶葓D必須加上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') # 注:?jiǎn)瓮ǖ阑叶葓D必須加上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') # 注:?jiǎn)瓮ǖ阑叶葓D必須加上cmap='gray'才能正確顯示 plt.show()

到此這篇關(guān)于詳解python中讀取和查看圖片的6種方法的文章就介紹到這了,更多相關(guān)python讀取和查看圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何不用安裝python就能在.NET里調(diào)用Python庫
這篇文章主要介紹了如何不用安裝python就能在.NET里調(diào)用Python庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python實(shí)現(xiàn)進(jìn)程同步和通信的方法
本篇文章主要介紹了Python實(shí)現(xiàn)進(jìn)程同步和通信的方法,詳細(xì)的介紹了Process、Queue、Pipe、Lock等組件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
Django創(chuàng)建一個(gè)后臺(tái)的基本步驟記錄
這篇文章主要給大家介紹了關(guān)于Django創(chuàng)建一個(gè)后臺(tái)的基本步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python?ConfigParser庫輕松讀寫INI文件實(shí)例探究
這篇文章主要為大家介紹了Python?ConfigParser庫輕松讀寫INI文件實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python面經(jīng)之16個(gè)高頻面試問題總結(jié)
這篇文章主要給大家介紹了關(guān)于Python面經(jīng)之16個(gè)高頻面試問題的相關(guān)資料,幫助大家回顧基礎(chǔ)知識(shí),了解面試套路,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
Python利用公共鍵如何對(duì)字典列表進(jìn)行排序詳解
這篇文章主要給大家介紹了關(guān)于Python利用公共鍵如何對(duì)字典列表進(jìn)行排序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05

