如何根据 Excel 文件中的编号查找图像?

如何根据 Excel 文件中的编号查找图像?

我的一个文件夹中有 1,015 张图像,这些图像被分为七种类型,这些类型是皮肤癌的医学诊断类别,相同的图像在 Excel 文件中按编号分类(每张图片的编号对应于该图像的诊断类型)。显然,每张图像都有一个与诊断类型相对应的特定编号,我的问题是如何按组对文件夹中的这些图像进行排序,以便每组图像具有相同的诊断。

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

答案1

您可以在 powershell 中尝试类似以下的方法 它当然可以进行优化,但我希望它通过 2 个步骤简单易懂: - 第一个:根据您的输入文件 (.csv) 创建目标树 - 第二个:在相应的子目录中移动图像文件)


#The process : 
$BasedImages = "\\path\to\ImagesDirectory"
$csvfile = "\\path\to\Inputcsvfile.csv"

#Gather all subfolders in the Images Directory (only first level) and put the name of these folder in a var. The only property useful for later use is the Directory name. Useless to gather all properties 
$ExistingSubDir = Get-ChildItem -Path $BasedImages -Directory | Select-Object -Property name

# Gather unique diagnosis in the input file and put in a var. The only useful property in the dx property for a later use. Useless to collect more info. 
$UniqueDiagnosis = Import-Csv -Path $csvfile | Select-Object -property dx -Unique

# gather all images files FullName in the Images Directory and put in a var. it seems that only  Name,DirectoryName, FullName properties will be usefull for later use
$AllImagesFiles = Get-ChildItem -Path $BasedImages -File | Select-Object -Property Name, DirectoryName FullName

# now First Step : build a Tree with  subfolders named by the unique Diagnosis name. 
foreach ($Diagnosis in $UniqueDiagnosis)
    { 
    # search if a diagnosis dir name (dx field in the input .csv file) exist in the ImageDirectory and put the result in a var
    if ($ExistingSubDir -contains $Diagnosis) 
        { 
        Write-Host "$ExistingSubDir is still existing, no action at this step" -ForegroundColor Green 
        }
    else
        {
        New-Item -Path $BasedImages -Name $Diagnosis -ItemType Directory
        Write-Host "a sub-directory named $Diagnosis has been created in the folder $BasedImages" -ForegroundColor Yellow
        }
    }

# At this step, you'll have some sub directories named with the name of all diagnosis (fied dx in the input file)
# Now Step 2 Time to move the files in the root folder
foreach ($image in $AllImagesFiles)
    { 
    $TargetSubDir = Get-Item -Path $($image.fullName)
    Move-Item -Path $($Image.FullName) -Destination ( Join-Path -Path (Split-Path -Path $($Image.DirectoryName) -Parent) -ChildPath $TargetSubDir)
    Write-Host "the image named $($image.name) has been moved to the sud directory $TargetSubDir" -ForegroundColor Green
    }

小心,我还没有完全测试代码,请谨慎使用。

奥利夫

相关内容