user1977802
user1977802

Reputation: 353

Subsetting Images out of a Folder

We have a folder with 4000 images. We would like to subset out a set of images into a new folder. We have a text file with a list of all of the images we would like to subset out. Is there an easy way to do this in windows or R?

Upvotes: 0

Views: 153

Answers (2)

Mikael Poul Johannesson
Mikael Poul Johannesson

Reputation: 1349

Let's say you have a images.txt file with the filenames of the images you want to subset (each on a new line), then you can import the list with

images <- readLines("images.txt")

and make a new director and copy your subset with

dir.create("subset")
for (i in seq_along(images)) {
  file.copy(images[i], paste0("subset/", images[i]))
}

assuming your working directory is the folder with the images, e.g. with setwd() or using the here package.

Upvotes: 1

JayCe
JayCe

Reputation: 251

This should work (not tested) with n the sample size:

n=100
newdir <- "C:\\Documents\\R\\wd\\text"
myfiles <- list.files()
mysample <- sample(myfiles,n)
file.copy(mysample,newdir)

See also this answer on moving files

Upvotes: 1

Related Questions