Skip to content

Instantly share code, notes, and snippets.

@simecek
Created August 18, 2017 11:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simecek/52d02fedf21a0f7d8a4b9d454272d5b4 to your computer and use it in GitHub Desktop.
Save simecek/52d02fedf21a0f7d8a4b9d454272d5b4 to your computer and use it in GitHub Desktop.
Classification of jpg files into several one letter subfolders
# example of usage
setwd("c:/Users/simecekp/Downloads/")
fls <- dir(patter=".*jpg")
jpg_sort(fls,c("a", "s"))
library(keypress)
library(jpeg)
jpg_sort <- function(files, keys, move.files=FALSE) {
## checking intputs
# all keys should be of length 1
stopifnot(nchar(keys)==1)
# all keys should be unique
stopifnot(length(keys)==length(unique(keys)))
# all files should exist
stopifnot(file.exists(files))
# number of files > 0
stopifnot(length(files)>0)
# number of keys > 1
stopifnot(length(keys)>1)
## creating folders
for (k in keys) {
if (!dir.exists(k)) dir.create(k)
}
## copy or move?
if (move.files) {
file.proc <- file.rename
} else {
file.proc <- file.copy
}
for (i in seq_along(files)) {
f = files[i]
jpeg <- readJPEG(f, native=TRUE)
plot(0:1, 0:1, type="n", ann=FALSE, axes=FALSE)
rasterImage(jpeg,0,0,1,1)
Sys.sleep(0.1)
x <- keypress()
while (!(x %in% keys)) x <- keypress()
file.proc(f, paste0(x,"/"))
}
}
@simecek
Copy link
Author

simecek commented Aug 18, 2017

keypress does not work in RStudio, needs to be run in terminal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment