目录
最常见的三种图片格式
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()