file.copy:尝试将一批图像复制到不同的文件夹中

问题描述 投票:0回答:2

我目前正在尝试编写一个代码,该代码将读取文件列表,确认它是图像,然后将此文件复制到其正确的 IMG 文件夹中。目前目录的设置按月将图像存放在一个文件夹中,该目录中包含松散的图像,以及使用 phenopix 中的 StructureFolder 函数设置的其他 4 个文件夹。

我当前正在使用 for 循环,并检查(使用 stringr 包)图像的名称是否包含“.jpg”,如果为 True,我希望将此文件复制到使用 StructureFolder 包创建的 IMG 文件夹中,这是(我的假设)该特定目录的文件夹路径的快捷方式。我不确定我做错了什么,但我知道 if 语句工作正常,因为我已经使用简单的打印语句对其进行了测试,只是 file.copy 没有被正确读取。非常感谢任何帮助!

# here is what I have currently wd <- "~/Desktop/codes/phenocam/DSM/2023/02" > wd <- "~/Desktop/codes/phenocam/DSM/2023/02" files <- list.files(wd) print(files) > files <- list.files(wd) > print(files) [1] "2023-02-01_10_30_00.jpg" "2023-02-01_11_00_00.jpg" [3] "2023-02-01_11_30_00.jpg" "2023-02-01_12_00_00.jpg" [5] "2023-02-01_12_30_00.jpg" "2023-02-01_13_00_00.jpg" # ommitted the majority of file names since it is repetitive my_path = structureFolder(wd, showWarnings = F) > my_path = structureFolder(wd, showWarnings = F) Put all your images in ~/Desktop/codes/phenocam/DSM/2023/02/IMG/ Put your reference image in ~/Desktop/codes/phenocam/DSM/2023/02/REF/ Draw your ROI with DrawROI(): set path_img_ref to ~/Desktop/codes/phenocam/DSM/2023/02/REF/ set path_ROIs to ~/Desktop/codes/phenocam/DSM/2023/02/ROI/ Then you can extractVIs(): set img.path as ~/Desktop/codes/phenocam/DSM/2023/02/IMG/ set roi.path as ~/Desktop/codes/phenocam/DSM/2023/02/ROI/ set vi.path to ~/Desktop/codes/phenocam/DSM/2023/02/VI/ ------------------------ Alternatively, assign this function to an object and use named elements of the returned list for (file in files) { if (stringr::str_detect(file, '.jpg')) { file.copy(from = wd, to = my_path$img) } }
for循环后没有任何输出,检查IMG文件夹显示没有图像被复制过来。再次,非常感谢任何帮助(以及任何使它变得更简单的建议,因为我是一个新的(ish)编码器!谢谢:)

r for-loop directory copy stringr
2个回答
0
投票
欢迎。请努力提供可重现的示例,因为这对任何想要帮助您的人都有帮助。

我希望我没有做到这一点,但您应该能够使用这个示例从这里进行构建。

# Create list of files and path, include recursive = TRUE only if you want to search sub-directories; use "pattern" to filter to only image files my_files <- as_tibble(list.files(recursive = TRUE, full.names = TRUE, pattern = "jpg")) %>% # this column brings in only the file name (assuming no sub-folders) cbind(as_tibble(list.files(recursive = TRUE, full.names = FALSE, pattern = "jpg"))) # rename columns colnames(my_files) <- c("full_path_name", "file_name") # create lists of each name my_files_full_path_name <- unlist(my_files$full_path_name) my_files_short_name <- unlist(my_files$file_name) # copy jpeg files to my IMG file file.copy(from = my_files_full_path_name, to = paste("IMG/",my_files_short_name), overwrite = TRUE, recursive = FALSE, copy.mode = TRUE)
请记住,list.files 和 file.copy 是矢量化函数,因此不需要循环。

希望这有帮助。


0
投票
对不起!我也尝试在 phenopix 中使用函数 DrawMULTIROI() 。它应该输出一个包含 ROI 坐标和其他特征的“.R”文件以及一个包含多边形的“.jpg”文件,但只存储了 jpg 文件。这是我按照教程的时候出现的问题,关于phenopix使用的讨论很少,所以如果您能回复我,我将不胜感激!

© www.soinside.com 2019 - 2024. All rights reserved.