-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-1. Pre-processing image (train).R
51 lines (36 loc) · 1.32 KB
/
2-1. Pre-processing image (train).R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
library(jpeg)
library(OpenImageR)
file_path <- 'monet2photo'
train_list <- list()
train_monet_path <- paste0(file_path, '/trainA')
files <- list.files(train_monet_path, full.names = TRUE)
train_list[[1]] <- list()
pb <- txtProgressBar(max = length(files), style = 3)
for (i in 1:length(files)) {
img <- readJPEG(files[i])
if (all.equal(dim(img), c(256, 256, 3)) == TRUE) {
train_list[[1]][[i]] <- readBin(con = files[i], what = 'raw', n = file.size(files[i]))
} else {
resized_img <- resizeImage(img, width = 256, height = 256, method = 'bilinear')
train_list[[1]][[i]] <- writeJPEG(resized_img)
}
setTxtProgressBar(pb, i)
}
close(pb)
train_photo_path <- paste0(file_path, '/trainB')
files <- list.files(train_photo_path, full.names = TRUE)
train_list[[2]] <- list()
pb <- txtProgressBar(max = length(files), style = 3)
for (i in 1:length(files)) {
img <- readJPEG(files[i])
if (all.equal(dim(img), c(256, 256, 3)) == TRUE) {
train_list[[2]][[i]] <- readBin(con = files[i], what = 'raw', n = file.size(files[i]))
} else {
resized_img <- resizeImage(img, width = 256, height = 256, method = 'bilinear')
train_list[[2]][[i]] <- writeJPEG(resized_img)
}
setTxtProgressBar(pb, i)
}
close(pb)
names(train_list) <- c('monet', 'photo')
save(train_list, file = paste0(file_path, '/train_list.RData'))