简单图像处理(图片->二进制->图片)

  1. #coding=utf-8  
  2.   
  3. ''''' 
  4. 1-将图片转化为数组并存为二进制文件 
  5. 2-从二进制文件中读取数并重新恢复为图片 
  6. '''  
  7.   
  8. from __future__ import print_function  
  9. import numpy  
  10. import PIL.Image   
  11. import pickle  
  12. import matplotlib.pyplot  
  13. import pdb  
  14.   
  15. class Operation(object):  
  16.     image_base_path = "../image/"  
  17.     data_base_path = "../data/"  
  18.   
  19.     def image_to_array(self,filenames):  
  20.         """ 
  21.         将图片转化为数组并存为二进制文件 
  22.         """  
  23.         n = filenames.__len__()#获取图片个数  
  24.         result = numpy.array([]) #创建一个空的一维数组  
  25.         print("开始将图片转化为数组")  
  26.         for i in range(n):  
  27.             image = PIL.Image.open(self.image_base_path+filenames[i])  
  28.             r,g,b = image.split() # rgb通道分离  
  29.             # 注意:下面一定要reshpae(1024)使其变为一维数组,否则拼接的数据会出现错误,导致无法恢复图片  
  30.             r_arr = numpy.array(r).reshape(1024)  
  31.             g_arr = numpy.array(g).reshape(1024)  
  32.             b_arr = numpy.array(b).reshape(1024)  
  33.             # 行拼接,类似于接火车;最终结果:共n行,一行3072列,为一张图片的rgb值  
  34.             image_arr = numpy.concatenate((r_arr,g_arr,b_arr))  
  35.             result = numpy.concatenate((result,image_arr))  
  36.   
  37.         result = result.reshape(n,3072# 将一维数组转化为n行3072列的二维数组  
  38.         print("转化数组over,开始保存为文件")  
  39.         file_path = self.data_base_path + 'data2.bin'  
  40.         with open(file_path,mode='wb') as f:  
  41.             pickle.dump(result,f)  
  42.         print("保存成功")  
  43.   
  44.     def array_to_image(self,filename):  
  45.         ''''' 
  46.         从二进制文件中读取数据并重新恢复为图片 
  47.         '''  
  48.         with open(self.data_base_path + filename,mode='rb') as f:  
  49.             arr = pickle.load(f) #加载并反序列化数据  
  50.         rows = arr.shape[0#rows=5  
  51.         #pdb.set_trace()  
  52.         #print("rows:",rows)  
  53.         arr = arr.reshape(rows,3,32,32)  
  54.         print(arr)<span style="white-space:pre;">   </span>#打印数组  
  55.         for index in range(rows):  
  56.             a = arr[index]  
  57.             #得到RGB通道  
  58.             r = PIL.Image.fromarray(a[0]).convert('L')  
  59.             g = PIL.Image.fromarray(a[1]).convert('L')  
  60.             b = PIL.Image.fromarray(a[2]).convert('L')  
  61.             image = PIL.Image.merge("RGB",(r,g,b))  
  62.             #显示图片  
  63.             matplotlib.pyplot.imshow(image)  
  64.             matplotlib.pyplot.show()  
  65.             #image.save(self.image_base_path + "result" + str(index) + ".png",'png')  
  66.   
  67. if __name__ == "__main__":  
  68.     my_operator = Operation()  
  69.     images = []  
  70.     for j in range(5):  
  71.         images.append(str(j) + ".png")  
  72. #   my_operator.image_to_array(images)  
  73.     my_operator.array_to_image('data2.bin')  

猜你喜欢

转载自blog.csdn.net/z_x_xing_/article/details/79785385