python 灰度图的保存方式_python+opencv解决方案:保存图像的方法比较之一

学习各种图片读入和保存方法,分析对比其图像的格式

1.cv2打开和保存图片:分彩色和灰度图两种情况

2.PIL打开和保存图片:

3.skimage打开和保存图片:分彩色和灰度图两种情况

4.plt打开和保存图片:

cv2 open and cvtBGR (shape,dtype):(200, 280, 3),uint8 ski open (shape,dtype):(200, 280, 3),uint8 plt open (shape,dtype):(200, 280, 3),uint8 pil open (size ,dtype):(280, 200),JPEG pil open and pil2arr (shape,dtype):(200, 280, 3),uint8 pil open and arr2pil (size ,dtype):(280, 200),None cv2 open and save (shape,dtype):(200, 280, 3),uint8 pil open and save (shape,dtype):(200, 280, 3),uint8 ski open and save (shape,dtype):(200, 280, 3),uint8 plt open and save (shape,dtype):(288, 432, 3),uint8 cv2 open gray (shape,dtype):(200, 280),uint8 pil open gray (size ,dtype):(280, 200),None ski open gray (shape,dtype):(200, 280),float64 cv2 open and savegray(shape,dtype):(200, 280),uint8 pil open and savegray(shape,dtype):(200, 280),uint8 ski open and savegray(shape,dtype):(200, 280),uint8

''' 1.cv2打开图片,显示后,用cv2存储 ''' import cv2 import matplotlib.pyplot as plt # cv2读入和保存彩色图 img_cv2BGR = cv2.imread('house.jpg') img_cv2RGB = cv2.cvtColor(img_cv2BGR,cv2.COLOR_BGR2RGB) cv2.imwrite('house_cv2write.jpg',img_cv2RGB) # 图像占内存略大于原图 img_cv2write = plt.imread('house_cv2write.jpg') print(img_cv2BGR.dtype) print(img_cv2BGR.size) print(img_cv2BGR.shape) print(img_cv2BGR) # cv2读入和保存灰度图 imgray_cv2 = cv2.imread('house.jpg',cv2.IMREAD_GRAYSCALE) cv2.imwrite('housegray_cv2write.jpg',imgray_cv2) imgray_cv2write = plt.imread('housegray_cv2write.jpg') print(imgray_cv2.dtype) print(imgray_cv2.size) print(imgray_cv2.shape) print(imgray_cv2) '''2.PIL保存''' from PIL import Image import numpy as np # 用PIL读入和保存彩色图,并进行格式转换以用于不同目的 img_pil = Image.open('house.jpg') # img类,JPEG格式,mode=RGB img_pil2arr = np.array(img_pil) # 转成ndarray img_arr2pil = Image.fromarray(img_pil2arr) # 矩阵再转为图像 img_arr2pil.save('house_pilsave.jpg') # PIL保存图像 img_pilsave = plt.imread('house_pilsave.jpg') print(img_pil.format) # JPEG; 'Image' object has no attribute 'shape' and 'dtype' print(img_pil.size) # (280, 200) print(img_pil) #

Python

你可能感兴趣的:(python,灰度图的保存方式)