使用矩阵存图,读图

#把30张图片存成矩阵
import numpy as np,os
import PIL.Image as pim#处理图片的工具库
import pickle as pic#矩阵的序列化和反序列化
class imagew:
    def imageToArray(self,dir):
        image_list=[];
        sdir=os.listdir(dir)

        for i in sdir:
            path=os.path.join(dir,i)
            im=pim.open(path,"r")
            r,g,b=im.split()#得到一张图片的rgb值
            rgb1=np.array(r).reshape((921600,))
            rgb2=np.array(g).reshape((921600,))
            rgb3=np.array(b).reshape((921600,))
            timage=np.hstack((rgb1,rgb2,rgb3))#一张图片

            fiimalist=np.concatenate((image_list,timage))
        allimage=fiimalist.reshape(len(sdir),2764800)

        with open("image.bat",mode="wb") as f:
            pic.dump(fiimalist,f)
        os.close(1)


    @staticmethod
    def readImage(file):
        f=open(file,"rb")
        allimage=pic.load(f)
        # image_count=allimage.shape[0]#图片个数
        all_array=np.reshape(allimage,(3,1280,720))
        for i in range(0,0):

            r=pim.fromarray(all_array[i][0],mode="RGB")
            g=pim.fromarray(all_array[i][0],mode="RGB")
            b=pim.fromarray(all_array[i][0],mode="RGB")
            imares=pim.merge("RGB",[r,g,b])
            imares.show()
            pim.save("result%s.jpg","jpeg")
class test:
    if __name__ == '__main__':
        i = imagew()
        i.imageToArray("image")
        i.readImage("image.bat")

  

原文地址:https://www.cnblogs.com/huiandong/p/9550963.html