圖像的讀取、存儲、類型轉換

目錄

1、matplotlib讀取與顯示圖片

2、用cv2讀取與顯示圖片

3、PIL.Image讀取圖片

4、總結:


最常見的三種圖片格式

str_bmp = r"../Data_set/00000_sr.bmp"
str_jpg = r"../Data_set/00205.jpg"
str_png = r"../Data_set/0901x2.png"

1、matplotlib讀取與顯示圖片

import matplotlib.pyplot as plot
import matplotlib.image as pl_image
def matplot_dealwith(str):
    in_image = pl_image.imread(str)  # 類型numpy.array   PLT默認讀取圖片數據格式:(高,寬,通道(R,G,B))。
    plot.imshow(in_image)
    print(in_image.shape)   # (678, 1020, 3)
    plot.show()

2、用cv2讀取與顯示圖片

def cv2_dealwith(str):
    img = cv2.imread(str)  # 打開圖像,opencv默認讀取圖片的數據爲: (高,寬,通道(B,G,R))。
    # img的類型: numpy.array
    print(img.shape)  # 圖像通道順序爲:BGR注意0-0
    cv2.imshow("image", img)  # 顯示圖片,opencv默認讀取圖片的數據爲: 注意0-0(高,寬,通道(B,G,R))。
    cv2.waitKey(0)

cv2.imshow()輸入是(R,G,B)通道,則出現下面這種情況,圖像色彩混亂

3、PIL.Image讀取圖片

from PIL import Image
import numpy as np
def pil_dealwith(str):
    in_image = Image.open(str)  # RGB(三通道)或者L(單通道灰度)
    print(type(in_image))  # 數據類型 <class 'PIL.PngImagePlugin.PngImageFile'>
    # in_image.save() #存圖片
    np_img = np.array(in_image)   # pil 轉numpy
    print(type(np_img.shape))
    print(np_img.shape)
    pil_img = Image.fromarray(np_img)   # numpy轉 pil
    print(type(pil_img))
    in_image.show()

 

4、總結:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章