1. 程式人生 > >使用矩陣存圖,讀圖

使用矩陣存圖,讀圖

pickle nat load cat con pat pic __main__ %s

#把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")

  

使用矩陣存圖,讀圖