Python各种图像库的图像读写方式的简单介绍(附代码)
本篇文章给大家带来的内容是关于python各种图像库的图像读写方式的简单介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
目前主流的图像库有几下几种:
1. OpenCV 2. PIL(Pillow) 3. matplotlib.image 4. skimage 5. scipy.misc
结论:以上图片库中当属OpenCV最为强大,成熟。
1.1 OpenCV 图像的读取与储存
立即学习“Python免费学习笔记(深入)”;
import cv2#读取图像 直接是numpy矩阵格式img = cv2.imread('horse.jpg',1) # 0表示读入灰色图片,1表示读入彩色图片cv2.imshow('image',img) # 显示图像print(img.shape) # (height,width,channel)print(img.size) # 像素数量print(img.dtype) # 数据类型print(img) # 打印图像的numpy数组,3纬数组#储存图像# 当前目录储存cv2.write(‘horse1.jpg',img)# 自定义储存cv2.write(‘/path_name/’ + str(image_name) + '.jpg',img) cv2.waitKey()
1.2OpenCV 图像灰化处理
import cv2#方法一img = cv2.imread('horse.jpg',0) # 0表示读入灰色图片,或者使用cv2.IMREAD_GRATSCALE 替代0cv2.imshow('gray image',img)#方法二img = cv2.imread('horse.jpg')gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)cv2.imshow('gray image',gray_img)print(gray_img.shape) # (height, width)print(gray_img.size) # 像素数量print(gray_img) # 打印图像的numpy数组,2维cv2.waitKey()
1.3 OpenCV 矩阵格式变换
Why?:OpenCV的矩阵格式 (height, width, channels) -->> 深度学习矩阵类型可能是 (channels,height,width)
import cv2import numpy as npimg = cv2.imread('horse.jpg',1)cv2.imshow('image',img)# 矩阵格式的变换print(img.shape)img = img.transpose(2,0,1) #变换函数 print(img.shape)
# 矩阵扩展 (batch_size, channels, height, width) 预测单张图片的操作# 加一列作为图片的个数img = np.expand_dims(img, axis=0) #使用numpy函数print(img.shape)
# 训练阶段构建batchdata_lst = []loop: img = cv2.imread('xxx.jpg') data_lst.append(img)data_arr = np.array(data_lst)
1.4 OpenCV 图片归一化 (Data Normalization)
import cv2# 为了减少计算量,需要把像素值0-255转换到0-1之间img = cv2.imread('horse.jpg')img = img.astype('float') / 255.0 # 先转化数据类型为floatprint(img.dtype)print(img)
1.5 OpenCV BRG转换为RGB
import cv2img = cv2.imread('horse.jpg')img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # 转为RGB formatprint(img)
1.6 OpenCV 访问像素点
import cv2img = cv2.imread('horse.jpg')gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 转为Gray imageprint(img[4,4]) # 3 channelsprint(gray_img[4,4]) # 1 channel
1.7 OpenCV 感兴趣区域剪切(ROI)
import cv2img = cv2.imread('horse.jpg')print(img.shape)roi = img[0:437,0:400] # [y:height,x:width]cv2.imshow('roi',roi)cv2.waitKey()
2.1 PIL 图像读取与储存
from PIL import Imageimport numpy as np#图像读取img = Image.open('horse.jpg')print(img.format) # 图片格式print(img.size) # (width,height)print(img.mode) # 图片通道类型#将图像转化为矩阵格式arr = np.array(img)print(arr.shape)print(arr.dtype)#图像储存new_img = Image.fromarray(arr)new_img.save('test.jpg')img.show()
2.2 PIL 图像灰化处理
#图像灰化处理gray = Image.open('horse.jpg').convert('L')gray_arr = np.array(gray)print(gray_arr.shape) # (height,width)print(gray_arr.dtype)print(gray_arr)gray.show()
2.3 PIL 感兴趣区域剪切
# 感兴趣区域剪切img = Image.open('horse.jpg')roi = img.crop((0,0,200,200)) # (左上x,左上y,右下x,右下y)roi.show()
2.4 通道操作
# 通道处理r,g,b = img.split() #分离img = Image.merge("RGB",(b,g,r)) #合并img = img.copy() #复制
3.1 Matplotlib 读取和存储图片
import matplotlib.pyplot as pltimport numpy as np# 图像读取为numpy数组格式img = plt.imread('horse.jpg')plt.axis('off') # 关闭刻度显示print(img.shape) # (height, width, channel)print(img.size) # 像素数量print(img.dtype)#储存图片plt.savefig('./name.jpg')figure = plt.figure(figsize=(20,10)) # 调整显示图片的大小plt.imshow(img)plt.show()
3.2 Matplotlib 图片灰化处理
#图片灰化处理# 平均值发img_mean = img.mean(axis=2)plt.imshow(img_mean,cmap='gray')plt.show()#最大值法img_max = img.max(axis=-1)plt.imshow(img_max,cmap='gray')plt.show()#RGB三原色法gravity = np.array([0.299,0.587,0.114])img_gravity = np.dot(img,gravity)plt.imshow(img_gravity,cmap="gray")plt.show()
4.1 skimage 读取和储存图像
from skimage import io#读取图像numpy数组格式img = io.imread('horse.jpg')print(img.shape)print(img.dtype)print(img.size)#print(img)io.imshow(img)#储存图像io.imsave('test.jpg',img)
4.2 skimage 灰化处理
#图像灰化处理并归一化img = io.imread('horse.jpg',as_gray=True)print(img.shape)print(img.dtype) # 数据类型位floatprint(img.size)print(img)io.imshow(img)io.show()
5.1 scipy.misc 读取和储存图像
#在1.2.0 之后统一用imageio模块import imageioimport matplotlib.pyplot as plt#读取图片为numpy数组img = imageio.imread('horse.jpg')print(img.dtype)print(img.size) # 像素数量print(img.shape) #(height, width, channels)plt.imshow(img)plt.show()print(img)#储存图片imageio.imsave('test.jpg',img)