基于python讀取圖像的幾種方式匯總
本文介紹幾種基于python的圖像讀取方式:
- 基于PIL庫的圖像讀取、保存和顯示
- 基于opencv-python的圖像讀取、保存和顯示
- 基于matplotlib的圖像讀取、保存和顯示
- 基于scikit-image的圖像讀取、保存和顯示
- 基于imageio的圖像讀取、保存和顯示
安裝方式基本使用pip即可:
pip install pillow pip install scikit-image pip install matplotlib pip install opencv-python pip install numpy scipy scikit-learn
基于PIL庫的圖像讀取、保存和顯示
from PIL import Image
設(shè)置圖片名字
img_path = './test.png'
用PIL的open
函數(shù)讀取圖片
img = Image.open(img_path)
讀進(jìn)來是一個(gè)Image對(duì)象
img
查看圖片的mode
img.mode
'RGB'
用PIL函數(shù)convert
將彩色RGB圖像轉(zhuǎn)換為灰度圖像
img_g = img.convert('L')
img_g.mode
'L'
img_g.save('./test_gray.png')
使用PIL庫的crop
函數(shù)可對(duì)圖像進(jìn)行裁剪
img_c = img.crop((100,50,200,150))img_c
圖像旋轉(zhuǎn)
img.rotate(45)
在圖像上添加文字
from PIL import ImageDraw, ImageFont draw = ImageDraw.Draw(img) font = ImageFont.truetype('/home/fsf/Fonts/ariali.ttf',size=24) draw.text((10,5), "This is a picture of sunspot.", font=font) del draw img
基于opencv-python的圖像讀取、保存和顯示
import cv2
img = cv2.imread('./test.png')
使用cv2都進(jìn)來是一個(gè)numpy矩陣,像素值介于0~255
,可以使用matplotlib進(jìn)行展示
img.min(), img.max()
(0, 255)
import matplotlib.pyplot as plt plt.imshow(img) plt.axis('off') plt.show()
基于matplotlib的圖像讀取、顯示和保存
import matplotlib.image as mpimg
img = mpimg.imread('./test.png')
img.min(),img.max()
(0.0, 1.0)
像素值介于0~1
之間,可以使用如下方法進(jìn)行展示
import matplotlib.pyplot as plt plt.imshow(img,interpolation='spline16') plt.axis('off') plt.show()
注意:matplotlib在進(jìn)行imshow
時(shí),可以進(jìn)行不同程度的插值,當(dāng)繪制圖像很小時(shí),這些方法比較有用,如上所示就是用了樣條插值。
基于scikit-image的圖像讀取、保存和顯示
from skimage.io import imread, imsave, imshow
img = imread('./test.png')
這個(gè)和opencv-python類似,讀取進(jìn)來也是numpy矩陣,像素值介于0~255
之間
img.min(), img.max()
(0, 255)
import matplotlib.pyplot as plt plt.imshow(img,interpolation='spline16') plt.axis('off') plt.show()
基于imageio的圖像讀取、顯示和保存
import imageio
img = imageio.imread('./test.png')
img.min(), img.max()
(0, 255)
這個(gè)和opencv-python、scikit-image類似,讀取進(jìn)來也都是numpy矩陣,像素值介于0~255
之間
import matplotlib.pyplot as plt plt.imshow(img,interpolation='spline16') plt.axis('off') plt.show()
總結(jié)
到此這篇關(guān)于基于python讀取圖像的幾種方式的文章就介紹到這了,更多相關(guān)python讀取圖像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中的關(guān)鍵字參數(shù)*args和**kwargs詳解
這篇文章主要介紹了python中的關(guān)鍵字參數(shù)*args和**kwargs詳解,在定義類或函數(shù)時(shí),有時(shí)候會(huì)用到*args和**kwargs,前者叫位置參數(shù),后者叫關(guān)鍵字參數(shù),需要的朋友可以參考下2023-11-11python使用pika庫調(diào)用rabbitmq參數(shù)使用詳情
這篇文章主要介紹了python使用pika庫調(diào)用rabbitmq參數(shù)使用詳情,文章通過展開文章主題分享了三種方式,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08Python?IDLE?Subprocess?Connection?Error的簡(jiǎn)單解決方法
最近用要Python處理一點(diǎn)事,就打開Python IDLE,結(jié)果出現(xiàn)錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于Python?IDLE?Subprocess?Connection?Error的簡(jiǎn)單解決方法,需要的朋友可以參考下2023-01-01Python獲取系統(tǒng)所有進(jìn)程PID及進(jìn)程名稱的方法示例
這篇文章主要介紹了Python獲取系統(tǒng)所有進(jìn)程PID及進(jìn)程名稱的方法,涉及Python使用psutil對(duì)系統(tǒng)進(jìn)程進(jìn)行操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-05-05Python程序設(shè)計(jì)入門(3)數(shù)組的使用
這篇文章主要介紹了Python數(shù)組的使用方法,需要的朋友可以參考下2014-06-06Django零基礎(chǔ)入門之路由path和re_path詳解
這篇文章主要介紹了Django零基礎(chǔ)入門之路由path和re_path,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09