python如何實(shí)現(xiàn)讀取并顯示圖片(不需要圖形界面)
在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 這兩個(gè)庫操作圖片。本人偏愛 matpoltlib,因?yàn)樗恼Z法更像 matlab。
一、matplotlib
1. 顯示圖片
import matplotlib.pyplot as plt # plt 用于顯示圖片
import matplotlib.image as mpimg # mpimg 用于讀取圖片
import numpy as np
lena = mpimg.imread('lena.png') # 讀取和代碼處于同一目錄下的 lena.png
# 此時(shí) lena 就已經(jīng)是一個(gè) np.array 了,可以對它進(jìn)行任意處理
lena.shape #(512, 512, 3)
plt.imshow(lena) # 顯示圖片
plt.axis('off') # 不顯示坐標(biāo)軸
plt.show()
2. 顯示某個(gè)通道
# 顯示圖片的第一個(gè)通道
lena_1 = lena[:,:,0]
plt.imshow('lena_1')
plt.show()
# 此時(shí)會發(fā)現(xiàn)顯示的是熱量圖,不是我們預(yù)想的灰度圖,可以添加 cmap 參數(shù),有如下幾種添加方法:
plt.imshow('lena_1', cmap='Greys_r')
plt.show()
img = plt.imshow('lena_1')
img.set_cmap('gray') # 'hot' 是熱量圖
plt.show()
3. 將 RGB 轉(zhuǎn)為灰度圖
matplotlib 中沒有合適的函數(shù)可以將 RGB 圖轉(zhuǎn)換為灰度圖,可以根據(jù)公式自定義一個(gè):
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
gray = rgb2gray(lena)
# 也可以用 plt.imshow(gray, cmap = plt.get_cmap('gray'))
plt.imshow(gray, cmap='Greys_r')
plt.axis('off')
plt.show()
4. 對圖像進(jìn)行放縮
這里要用到 scipy
from scipy import misc
lena_new_sz = misc.imresize(lena, 0.5) # 第二個(gè)參數(shù)如果是整數(shù),則為百分比,如果是tuple,則為輸出圖像的尺寸
plt.imshow(lena_new_sz)
plt.axis('off')
plt.show()
5. 保存圖像
5.1 保存 matplotlib 畫出的圖像
該方法適用于保存任何 matplotlib 畫出的圖像,相當(dāng)于一個(gè) screencapture。
plt.imshow(lena_new_sz)
plt.axis('off')
plt.savefig('lena_new_sz.png')
5.2 將 array 保存為圖像
from scipy import misc
misc.imsave('lena_new_sz.png', lena_new_sz)
5.3 直接保存 array
讀取之后還是可以按照前面顯示數(shù)組的方法對圖像進(jìn)行顯示,這種方法完全不會對圖像質(zhì)量造成損失
np.save('lena_new_sz', lena_new_sz) # 會在保存的名字后面自動加上.npy
img = np.load('lena_new_sz.npy') # 讀取前面保存的數(shù)組
二、PIL
1. 顯示圖片
from PIL import Image
im = Image.open('lena.png')
im.show()
2. 將 PIL Image 圖片轉(zhuǎn)換為 numpy 數(shù)組
im_array = np.array(im) # 也可以用 np.asarray(im) 區(qū)別是 np.array() 是深拷貝,np.asarray() 是淺拷貝
3. 保存 PIL 圖片
直接調(diào)用 Image 類的 save 方法
from PIL import Image
I = Image.open('lena.png')
I.save('new_lena.png')
4. 將 numpy 數(shù)組轉(zhuǎn)換為 PIL 圖片
這里采用 matplotlib.image 讀入圖片數(shù)組,注意這里讀入的數(shù)組是 float32 型的,范圍是 0-1,而 PIL.Image 數(shù)據(jù)是 uinit8 型的,范圍是0-255,所以要進(jìn)行轉(zhuǎn)換:
import matplotlib.image as mpimg
from PIL import Image
lena = mpimg.imread('lena.png') # 這里讀入的數(shù)據(jù)是 float32 型的,范圍是0-1
im = Image.fromarray(np.uinit8(lena*255))
im.show()
5. RGB 轉(zhuǎn)換為灰度圖
from PIL import Image
I = Image.open('lena.png')
I.show()
L = I.convert('L')
L.show()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助
到此這篇關(guān)于python如何實(shí)現(xiàn)讀取并顯示圖片(不需要圖形界面)的文章就介紹到這了,更多相關(guān)Python 讀取并顯示圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python 實(shí)現(xiàn)一個(gè)計(jì)時(shí)器
- Python功能點(diǎn)實(shí)現(xiàn):函數(shù)級/代碼塊級計(jì)時(shí)器
- python實(shí)現(xiàn)屏保計(jì)時(shí)器的示例代碼
- python中的計(jì)時(shí)器timeit的使用方法
- python實(shí)現(xiàn)簡單的計(jì)時(shí)器功能函數(shù)
- python 實(shí)現(xiàn)一個(gè)圖形界面的匯率計(jì)算器
- 如何Tkinter模塊編寫Python圖形界面
- 健身房被搭訕?用python寫了個(gè)小米計(jì)時(shí)器助人為樂
相關(guān)文章
使用python實(shí)現(xiàn)下拉選擇框和頁簽的方法
ttk是Python中的一個(gè)模塊,它提供了一組用于創(chuàng)建GUI界面的工具和控件,這些控件包括按鈕、標(biāo)簽、文本框等,可以幫助開發(fā)者更方便地創(chuàng)建用戶界面,這篇文章主要介紹了使用python實(shí)現(xiàn)下拉選擇框和頁簽的方法,需要的朋友可以參考下2023-03-03
淺析Python+OpenCV使用攝像頭追蹤人臉面部血液變化實(shí)現(xiàn)脈搏評估
這篇文章主要介紹了Python+OpenCV使用攝像頭追蹤人臉面部血液變化實(shí)現(xiàn)脈搏評估,本文通過一段代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
python實(shí)現(xiàn)樹形打印目錄結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了python樹形打印目錄結(jié)構(gòu)的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

