How to display image stored in pandas dataframe?

前端 未结 2 790
太阳男子
太阳男子 2021-02-10 09:53
import pandas as pd
from scipy import misc
import numpy as np
import matplotlib.pyplot as plt

W = {\'img\':[misc.imread(\'pic.jpg\')]}
df = pd.DataFrame(W)

# This disp         


        
2条回答
  •  深忆病人
    2021-02-10 10:27

    It is not clear from the question why you would want to use pandas dataframes to store the image. I think this makes things unnecessarily complicated. You may instead directly store the numpy array in binary format and load it again at some point later.

    import numpy as np
    import matplotlib.pyplot as plt
    
    #create an image
    imar = np.array([[[1.,0.],[0.,0.]],
                     [[0.,1.],[0.,1.]],
                     [[0.,0.],[1.,1.]]]).transpose()
    plt.imsave('pic.jpg', imar)
    
    # read the image
    im = plt.imread('pic.jpg')
    # 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
    plt.imshow(new_im)
    plt.show()
    

    As a response to the comments below, which turn the question somehow in a different direction, you may surely store the path/name of the image in the dataframe.

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    #create an image
    imar = np.array([[[1.,0.],[0.,0.]],
                     [[0.,1.],[0.,1.]],
                     [[0.,0.],[1.,1.]]]).transpose()
    plt.imsave('pic.jpg', imar)
    
    #create dataframe
    
    df = pd.DataFrame([[0,""]], columns=["Feature1","Feature2"])
    
    # read the image
    im = plt.imread('pic.jpg')
    
    plt.imshow(im)
    plt.show()
    
    #save the image array to binary file
    np.save('mypic.npy', im)
    # store name of image in dataframe
    df.iloc[0,1] = 'mypic.npy'
    #save dataframe
    df.to_csv("mydf.csv")
    del df
    
    #read dataframe from csv
    df = pd.read_csv("mydf.csv")
    # load the image from binary file, given the path from the Dataframe
    new_im= np.load(df["Feature2"][0])
    # show the loaded image
    plt.imshow(new_im)
    plt.show()
    

    Last, you may go along the initally planned way of storing the actual image in a dataframe cell, but instead of writing to csv, you map pickle the dataframe, such that it can be read out just as if it had never been saved before.

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import pickle
    
    #create an image
    imar = np.array([[[1.,0.],[0.,0.]],
                     [[0.,1.],[0.,1.]],
                     [[0.,0.],[1.,1.]]]).transpose()
    plt.imsave('pic.jpg', imar)
    
    #create dataframe
    
    df = pd.DataFrame([[0,""]], columns=["Feature1","Feature2"])
    
    # read the image
    im = plt.imread('pic.jpg')
    
    plt.imshow(im)
    plt.show()
    
    # store the image itself  in dataframe
    df.iloc[0,1] = [im]
    #save dataframe
    pickle.dump(df, file("mydf.pickle", "wb"))
    del df
    
    #read dataframe from pickle
    df = pickle.load(file("mydf.pickle", "rb"))
    
    # show the loaded image from dataframe cell
    plt.imshow(df["Feature2"][0][0])
    plt.show()
    

提交回复
热议问题