将图像保存到csv文件,然后从csv文件读取

时间:2020-08-27 11:52:00

标签: python pandas numpy csv python-imaging-library

我已使用以下代码将图像转换为csv:

import pandas as pd
from PIL import Image 
import numpy as np 


image_array = []
for name in combined_df['path']:
    image_array.append(np.array(Image.open(name)))

image_df_1 = pd.DataFrame(image_array)     #Then coverted list to dataframe

image_df_1.to_csv('image.csv', index=False) # exported it to csv  (question 1)


csv_df = pd.read_csv('image.csv')  # exported csv using pandas   (question 2)

# I want to see images from csv file but there is problem

np.array(csv_df.iloc[0][0]).shape         # (question 3)
Output: ()      

# but if I see shape of dataframe before saving it to csv

np.array(image_df_1.iloc[0][0]).shape
output: (466, 806, 3)

我在做错什么吗?

  1. 将数据框保存到csv文件中吗?
  2. 还是在读取csv文件时?
  3. 还是在将值转换为数组时?
# data is available but not able to convert in from of array

csv_df.iloc[0][0]

Output: '[[[180 193 212]\n  [181 194 213]\n  [182 195 214]\n  ...\n  [177 190 209]\n  [177 190 209]\n  [177 190 209]]\n\n [[180 193 212]\n  [181 194 213]\n 

请告知。

1 个答案:

答案 0 :(得分:1)

简短答案:

答案是1:在保存到csv中时,多维DataFrame变成简单的字符串。因此,从csv读取后,DataFrame单元类型变为str。 =>从csv读取后,csv_df.iloc[0][0]的类型为str。但是image_df_1.iloc[0][0]的类型为list(3D嵌套列表)。

详细答案:

这是因为当您将多维DataFrame保存到csv中时,csv变成纯文本,然后当您将csv阅读到新DataFrame中时,新DataFrame不是多维的,它只是带有字符串单元格的2D DataFrame

因此,代码中csv_df.iloc[0][0]的类型是字符串,但是image_df_1.iloc[0][0]的类型是3D嵌套列表。

因此,您的完整答案是1和2的组合:它将字符串写入csv,然后在将csv转换为DataFrame时读取字符串。

看看我对代码的模拟:

>>> image_df_1 = pd.DataFrame([ [ [1, 2], [1, 2] ], [[1, 2], [1, 2] ], [ [1, 2], [1, 2] ], [[1, 2], [1, 2] ] ])  # a multidimensional DataFrame
>>> image_df_1.to_csv('image.csv', index=False)
>>> csv_df = pd.read_csv('image.csv')
>>> csv_df.iloc[0][0]
'[1, 2]'
>>> image_df_1.iloc[0][0]
[1, 2]
>>> type(csv_df.iloc[0][0])
<class 'str'>
>>> type(image_df_1.iloc[0][0])
<class 'list'>
>>> 

您会看到csv_df.iloc[0][0]的类型为str,而image_df_1.iloc[0][0]的类型为list