1、opencv
2、imageio
3、matplotlib
4、scipy
# coding:utf-8
import cv2
import imageio
from scipy import misc
from PIL import Image
from matplotlib import pyplot as plt
image_path = "./images/000011.jpg"
# 使用pillow讀取圖片,獲取圖片的寬和高
img_pillow = Image.open(image_path)
img_width = img_pillow.width # 圖片寬度
img_height = img_pillow.height # 圖片高度
print("width -> {}, height -> {}".format(img_width, img_height))
img_cv = cv2.imread(image_path)
img_imageio = imageio.imread(image_path)
img_scipy = misc.imread(image_path)
img_matplot = plt.imread(image_path)
print(img_cv.shape)
print(img_imageio.shape)
print(img_scipy.shape)
print(img_matplot.shape)
輸出結果如下:
width -> 2000, height -> 1333
(1333, 2000, 3)
(1333, 2000, 3)
(1333, 2000, 3)
(1333, 2000, 3)
注意事項:讀取出的圖像矩陣的shape是按 高度、寬度、通道數 這個順序,圖像寬度是第一個維度
補充:
在圖像處理中通常需要將圖像按照寬、高、通道順序進行存放,可以使用transpose方法進行轉換:
# 交換0 1 座標軸的數據,也就是交換高和寬的順序爲寬和高
img_transpose = img.transpose(1, 0, 2)
print(img_transpose.shape)
輸出結果如下:
(2000, 1333, 3)