如何根据数据帧的图像编号列来分隔文件夹中的图像?

2024-10-02 00:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的数据框形状(8683483),其中868是我的图像总数,3481是图像中的像素数。每一行代表一个特定的图像,图像编号在img列中。我应用了无监督学习,并将这些图像聚集在cluster列中。你知道吗

img cluster 0 1 2 3 4 5 6 7
0   3   1.0 1.0 1.0 1.0 1.0 1.0 1.0
1   2   1.0 1.0 1.0 1.0 1.0 1.0 1.0
2   3   1.0 1.0 1.0 1.0 1.0 1.0 1.0
3   1   1.0 1.0 1.0 1.0 1.0 1.0 1.0
5   3   1.0 1.0 1.0 1.0 1.0 1.0 1.0
6   3   1.0 1.0 1.0 1.0 1.0 1.0 1.0
8   3   1.0 1.0 1.0 1.0 1.0 1.0 1.0
9   3   1.0 1.0 1.0 1.0 1.0 1.0 1.0
10  2   1.0 1.0 1.0 1.0 1.0 1.0 1.0
11  2   1.0 1.0 1.0 1.0 1.0 1.0 1.0
13  3   1.0 1.0 1.0 1.0 1.0 1.0 1.0
15  1   1.0 1.0 1.0 1.0 1.0 1.0 1.0

我有一个文件夹,其中的图像标记与img列相同。现在,我想根据这些图像所属的簇来分离它们。你知道吗

例如 图像“0,2,5,6,8,9,13”属于cluster3,因此我想将这些图像分离到名为“cluster3”的子文件夹中,cluster1和cluster2也是如此。你知道吗

有没有简单的方法?你知道吗


Tags: 数据方法标记图像文件夹img代表编号
1条回答
网友
1楼 · 发布于 2024-10-02 00:22:37

您可以基于python中的os(或者Dennis评论的shutil模块移动文件,两者都可以工作)。据我所知,我们只关心img和cluster列

dictionary = df.set_index("img")["cluster"].to_dict()将返回一个字典,每个键是一个图像,每个簇是一个文件夹。 我不确定有多少个集群存在,但是我们也可以用os命令创建一些文件夹和子文件夹,如下所示

#This is where you decide to save the images 
#Here you make individual folders for each cluster
fp = "path/to/save/images/clusters/"
import os
os.mkdir("clusters/")
allClusters = list(set(df["cluster"]))
for x in allClusters:
    os.mkdir(fp+"cluster" + str(x))

然后,您可以将每个文件转到其相应的文件夹(我不确定每个文件的名称是什么,但现在我将假定名称是img1.png, img2.png ...等) 对于您的问题,我建议您重命名img列(或其他列,并将索引设置为下一行中的该列)

#This is where the dictionary is created. The key to each value is the 
#original file name
#The cluster value is the folder that each image will saved two (see above
#where we create each folder
dictionary = df.set_index("img")["cluster"].to_dict()
for x in dictionary:
    #THIS is how the file is acess, the dictionary stores the name of the
    #files as the key, and path to file is the folder of all those images
    filename = "path/to/images/" + str(x) + ".png" 

    #This is where we rename the original image to the new filepath
    os.rename(filename, fp + "cluster" + str(dictionary(x)) +"/"+ filename))

这应该可以胜任。如果有任何错误,请告诉我

相关问题 更多 >

    热门问题