import pickle,pprint
from PIL import Image
import numpy as np
import os
import matplotlib.image as plimg
class DictSave(object):
def __init__(self,filenames):
self.filenames = filenames
self.arr = []
self.all_arr = []
print
def image_input(self,filenames):
for filename in filenames:
self.arr = self.read_file(filename)
if self.all_arr==[]:
self.all_arr = self.arr
else:
self.all_arr = np.concatenate((self.all_arr,self.arr))
def read_file(self,filename):
im = Image.open(filename)
r, g, b = im.split()
r_arr = plimg.pil_to_array(r)
g_arr = plimg.pil_to_array(g)
b_arr = plimg.pil_to_array(b)
r_arr1 = r_arr.reshape(10800)
g_arr1 = g_arr.reshape(10800)
b_arr1 = b_arr.reshape(10800)
arr = np.concatenate((r_arr1, g_arr1, b_arr1))
return arr
def pickle_save(self,arr):
print ("正在存储")
contact = {'data': arr}
f = open('contact', 'wb')
pickle.dump(contact, f)
f.close()
print ("存储完毕")
if __name__ == "__main__":
filenames = [os.path.join("%d.png" % i) for i in range(0, 100)]
ds = DictSave(filenames)
ds.image_input(ds.filenames)
ds.pickle_save(ds.all_arr)
print ("最终数组的大小:"+str(ds.all_arr.shape))