循环图像,保存ID名称并将其相应地存储在数据框中
Looping images in, saving ID name and storing it correspondingly in dataframe
你好,
简介:
我正在尝试设置一个熊猫数据框以将多个离散的化学值连接到多个图像。这比我现在的水平高了一点点,所以我希望能在这里得到一些帮助。
到目前为止我得到了什么:
我目前从提供的数据表中切出两列,如下所示。
现在我这里有 1688 个数据点,并且有 1225 个大小为 10x10x4 (RGBA) 的图像要与之关联。 (1225,10,10,4) Uint8
这些图像都具有第 1 列中显示的相同 Sample_ID 名称。我的目标是 运行 一个循环,从文件夹中取出图像,将它们展平并重塑为 300x1然后将它们存储在根据 Sample_ID 检查的第 3 列中。这意味着正确的图像必须对应正确的 Sample_ID.
我已经在网上和此处的 Whosebug 上进行了搜索。我已经从这里尝试了 4 种不同的循环图像功能,但并没有给我预期的结果。
到目前为止,我最好的选择似乎是使用 glob 将其全部放入一个 numpy 文件中。但我肯定需要一个循环函数,将图像与相应的 id 和 Ni 值链接起来。
关于如何加载图像并存储其 ID 值以便与现有数据帧交叉引用的任何建议。
感谢您的宝贵时间。
假设图像 id 在其名称中并使用 matplotlib.image.imread
path = '.' # current directory
filenames = [os.path.abspath(os.path.join(path, x)) for x in os.listdir(path) if '.png' in x or '.jpg' in x]
>>> filenames
['image_0.png',
'image_1.png',
'image_2.png',
'image_3.png',
'image_4.png',
'image_5.png',
'image_6.png',
'image_7.png',
'image_8.png',
'image_9.png']
将图像读入数据框并将它们的名称添加为一列:
from matplotlib.image import imread
images_df = pd.DataFrame([[imread(filename).flatten()] for filename in filenames], columns=['images'])
images_df['id'] = filenames
images_df['id'] = images_df['id'].apply(os.path.basename)
>>> images_df
images id
0 [0.4627451, 0.05490196, 0.8745098, 0.79607844,... image_0.png
1 [0.20784314, 0.93333334, 0.73333335, 0.6156863... image_1.png
2 [0.4117647, 0.3254902, 0.8784314, 0.16470589, ... image_2.png
3 [0.8627451, 0.6862745, 0.78431374, 0.6431373, ... image_3.png
4 [0.44705883, 0.627451, 0.57254905, 0.78431374,... image_4.png
5 [0.7490196, 0.007843138, 0.25490198, 0.1372549... image_5.png
6 [0.039215688, 0.14901961, 0.5882353, 0.5137255... image_6.png
7 [0.24705882, 0.94509804, 0.1882353, 0.38039216... image_7.png
8 [0.35686275, 0.047058824, 0.56078434, 0.062745... image_8.png
9 [0.8, 0.23921569, 0.99607843, 0.89411765, 0.23... image_9.png
从图像中提取 ID:
>>> images_df['id'] = images_df['id'].str.split('.').str[0]
0 image_0
1 image_1
2 image_2
3 image_3
4 image_4
5 image_5
6 image_6
7 image_7
8 image_8
9 image_9
Name: id, dtype: object
images_df['id']
如果Sample_ID
是1需要转成整数
加入数据帧:
pd.merge(images_df, new_data_rdy, left_on='id', right_on='Sample_ID')
你好,
简介:
我正在尝试设置一个熊猫数据框以将多个离散的化学值连接到多个图像。这比我现在的水平高了一点点,所以我希望能在这里得到一些帮助。
到目前为止我得到了什么:
我目前从提供的数据表中切出两列,如下所示。
现在我这里有 1688 个数据点,并且有 1225 个大小为 10x10x4 (RGBA) 的图像要与之关联。 (1225,10,10,4) Uint8
这些图像都具有第 1 列中显示的相同 Sample_ID 名称。我的目标是 运行 一个循环,从文件夹中取出图像,将它们展平并重塑为 300x1然后将它们存储在根据 Sample_ID 检查的第 3 列中。这意味着正确的图像必须对应正确的 Sample_ID.
我已经在网上和此处的 Whosebug 上进行了搜索。我已经从这里尝试了 4 种不同的循环图像功能,但并没有给我预期的结果。
到目前为止,我最好的选择似乎是使用 glob 将其全部放入一个 numpy 文件中。但我肯定需要一个循环函数,将图像与相应的 id 和 Ni 值链接起来。
关于如何加载图像并存储其 ID 值以便与现有数据帧交叉引用的任何建议。
感谢您的宝贵时间。
假设图像 id 在其名称中并使用 matplotlib.image.imread
path = '.' # current directory
filenames = [os.path.abspath(os.path.join(path, x)) for x in os.listdir(path) if '.png' in x or '.jpg' in x]
>>> filenames
['image_0.png',
'image_1.png',
'image_2.png',
'image_3.png',
'image_4.png',
'image_5.png',
'image_6.png',
'image_7.png',
'image_8.png',
'image_9.png']
将图像读入数据框并将它们的名称添加为一列:
from matplotlib.image import imread
images_df = pd.DataFrame([[imread(filename).flatten()] for filename in filenames], columns=['images'])
images_df['id'] = filenames
images_df['id'] = images_df['id'].apply(os.path.basename)
>>> images_df
images id
0 [0.4627451, 0.05490196, 0.8745098, 0.79607844,... image_0.png
1 [0.20784314, 0.93333334, 0.73333335, 0.6156863... image_1.png
2 [0.4117647, 0.3254902, 0.8784314, 0.16470589, ... image_2.png
3 [0.8627451, 0.6862745, 0.78431374, 0.6431373, ... image_3.png
4 [0.44705883, 0.627451, 0.57254905, 0.78431374,... image_4.png
5 [0.7490196, 0.007843138, 0.25490198, 0.1372549... image_5.png
6 [0.039215688, 0.14901961, 0.5882353, 0.5137255... image_6.png
7 [0.24705882, 0.94509804, 0.1882353, 0.38039216... image_7.png
8 [0.35686275, 0.047058824, 0.56078434, 0.062745... image_8.png
9 [0.8, 0.23921569, 0.99607843, 0.89411765, 0.23... image_9.png
从图像中提取 ID:
>>> images_df['id'] = images_df['id'].str.split('.').str[0]
0 image_0
1 image_1
2 image_2
3 image_3
4 image_4
5 image_5
6 image_6
7 image_7
8 image_8
9 image_9
Name: id, dtype: object
images_df['id']
如果Sample_ID
是1需要转成整数
加入数据帧:
pd.merge(images_df, new_data_rdy, left_on='id', right_on='Sample_ID')