如何显示存储在 pandas 数据框中的图像? [英] How to display image stored in pandas dataframe?

查看:79
本文介绍了如何显示存储在 pandas 数据框中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 displays the image
plt.imshow(df.img1[0])
plt.show()

df.to_csv('mypic.csv')
new_df= pd.read_csv('mypic.csv')

# This does not display the image
plt.imshow(new_df.img1[0])
plt.show()

当我尝试显示由csv文件加载的图像时,出现错误:图像数据无法转换为浮点型.但是,使用数据框df时,我能够正确显示图像.

When I try to display the image as loaded by the csv file I obtain the error: Image data can not convert to float. However, I was able to correctly display the image when using the dataframe df.

我怀疑将df存储到csv文件时数据类型出了问题.我该如何解决这个问题?

I suspect that something went wrong with the data type when I stored df onto a csv file. How would I fix this issue?

我要补充一点,我的主要目标是

edit: I should add that my main objective is to

  1. 将包含图像的熊猫数据框写入到csv文件中
  2. 从磁盘读取csv文件,而不是将整个数据帧存储在RAM中

推荐答案

从这个问题尚不清楚,为什么要使用pandas数据框存储图像.我认为这使事情变得不必要地复杂.相反,您可以直接以二进制格式存储numpy数组,并在以后的某个时刻再次加载.

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()

最后,您可能会按照最初计划的方式将实际图像存储在数据帧单元中,但是您无需对csv进行写操作,而是将数据帧映射为pickle,这样就可以像从未读取过一样读取数据之前保存过.

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()

这篇关于如何显示存储在 pandas 数据框中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆