pillow读取图片
from PIL import Image
import numpy as np
img_path = './Training/meningioma/M546.jpg'
# 读取图片
image = Image.open(img_path)
width, height = image.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image.mode))
print("图片的format为{}".format(image.format))
pixel = image.load()
img_arr = np.array(image)
print("图片数组大小为{}".format(img_arr.shape))
print("图片像素值最大为{}".format(np.max(img_arr)))
print("图片像素值最大为{}".format(np.min(img_arr)))
#显示图片
image.show()
#
matplotlib读取图片
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img_path = './Training/meningioma/M546.jpg'
# 打开PNG图片文件
png_image = mpimg.imread(img_path)
plt.imshow(png_image)
CV2读取图片
import cv2
import numpy as np
img_path = './Training/meningioma/M546.jpg'
# 读取图片
img = cv2.imread(img_path)
print(img.shape)
# mac显示图片有问题,会卡死,图片无法关闭,
注意点
如果在jupyter中,最好使用matplotlib就可以
因为如果使用PIL库,图片是以窗口的形式展示的
而cv2的结果是不显示,所以最好使用matploblib可以在jupyter中显示图片