我尝试用opencv、PIL、matplotlib.pyplot读取图片像素,将读取到的像素矩阵保存为图片,再读取时发现像素值有出入,如何精确地保存矩阵为图片呢?
from typing import Counter
import numpy as np
from PIL import Image
cc = Image.open('ccc.jpg') #打开同目录下的ccc.jpg图片
np.array(cc)
print(type(cc))
cc = np.array(cc)
print(type(cc))
print(cc.shape)
print(cc.dtype)
print(Counter(cc.flatten()))
cc = Image.fromarray(cc)
print(type(cc))
cc.save('cccc.jpg')
cc = Image.open('cccc.jpg')
print(type(cc))
cc = np.array(cc)
print(type(cc))
print(cc.shape)
print(cc.dtype)
print(Counter(cc.flatten()))
import matplotlib.pyplot as plt
cc = plt.imread('ccc.jpg')
print(type(cc))
print(cc.shape)
print(Counter(cc.flatten()))
plt.figure(figsize=(30,40),dpi=1)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1,bottom=0,left=0,right=1,hspace =0,wspace =0)
plt.margins(0,0)
plt.imshow(cc)
plt.axis('off')
plt.savefig('cccc.jpg')
cc = plt.imread('cccc.jpg')
print(type(cc))
print(cc.shape)
print(Counter(cc.flatten()))
import cv2
cc = cv2.imread('ccc.jpg')
print(type(cc))
print(cc.shape)
print(Counter(cc.flatten()))
cv2.imwrite('cccc.jpg', cc)
cc = cv2.imread('cccc.jpg')
print(type(cc))
print(cc.shape)
print(Counter(cc.flatten()))