
# https://www.globalsino.com/ICs/
# Unpack the LSWMD.pkl and then read and/or save single image(s)

import gzip
import pandas as pd
import pickle
import time


with open(r"C:\0Python\wafermap\LSWMD.pkl", "rb") as f:
    ImageList = pd.read_pickle(f)
    #print(ImageList)
    print(ImageList.iloc[[2, 3, 4, 5]])
    print(ImageList.iloc[[811440, 811441, 811442, 811443, 811444, 811445, 811446, 811447,811448,811449]])

    image_i = 811441
    print("Images without the header is:: ", ImageList.head(image_i))
    # Remove header
    Row1_WithHeader = ImageList.head(811456)
    singleImage =Row1_WithHeader.iloc[:,:].values
    print("singleImage is:: ", singleImage)
    print(singleImage)
    
    import numpy as np
    import matplotlib.pyplot as plt

    #create an image
    imar = np.array(singleImage[image_i][0]).transpose()
    plt.imsave(r"C:\GlobalSino2\ICs\images\4236LSWMD.png", imar)

    # read the image
    im = plt.imread(r"C:\GlobalSino2\ICs\images\4236LSWMD.png")
    # show the image
    plt.imshow(im)
    plt.show()

    #save the image array to binary file
    np.save('mypic', im)
    # load the image from binary file
    new_im= np.load('mypic.npy')
    # show the loaded image
    time.sleep(20)
    plt.imshow(new_im)
    plt.show()

    
