'Read image file pixel value and write it to a data frame, saved it to csv, when I reload, Image can't show
First I open images and get their pixel value, then write to a dataframe.
skin_df_balanced['pix'] = skin_df_balanced['path'].map(lambda x: np.asarray(Image.open(x).resize((SIZE,SIZE))))
Then I saved this dataframe to csv:
skin_df_balanced.to_csv('./skin_df_balanced_pixel_value_500.csv')
But when I reload the csv and tried to plot the images:
test = pd.read_csv('./skin_df_balanced_pixel_value_500.csv',index_col=0)
n_samples = 5
fig, m_axs = plt.subplots(7, n_samples, figsize = (4*n_samples, 3*7))
for n_axs, (type_name, type_rows) in zip(m_axs, test.sort_values(['type']).groupby('type')):
n_axs[0].set_title(type_name)
for c_ax, (_, c_row) in zip(n_axs, type_rows.sample(n_samples, random_state=1234).iterrows()):
c_ax.imshow(c_row['pix'])
c_ax.axis('off')
I found this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-abe099827f62> in <module>()
5 n_axs[0].set_title(type_name)
6 for c_ax, (_, c_row) in zip(n_axs, type_rows.sample(n_samples, random_state=1234).iterrows()):
----> 7 c_ax.imshow(c_row['pix'])
8 c_ax.axis('off')
4 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py in set_data(self, A)
692 not np.can_cast(self._A.dtype, float, "same_kind")):
693 raise TypeError("Image data of dtype {} cannot be converted to "
--> 694 "float".format(self._A.dtype))
695
696 if not (self._A.ndim == 2
TypeError: Image data of dtype <U629 cannot be converted to float
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
