-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
37 lines (31 loc) · 1.12 KB
/
preprocess.py
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
import os
import numpy as np
import tensorflow as tf
from tqdm import tqdm
# preprocess image
def preprocess(path, size=256, channels=3):
# prepare
sess = tf.Session()
file_list = os.listdir(path)
save_path = path + '_npy'
with sess.as_default():
for f in tqdm(file_list):
file_name = os.path.basename(f)
for i in ['.jpg', '.jpeg', '.png']:
file_name = file_name.rstrip(i)
# start preprocess the image
x = tf.read_file(path+'/'+f)
x_decode = tf.image.decode_jpeg(x, channels=channels)
img = tf.image.resize_images(x_decode, [size, size])
img = tf.cast(img, tf.float32) / 127.5 - 1
# save the preprocessed image
np.save(os.path.join(save_path, file_name), img.eval())
if __name__ == "__main__":
# make folders
os.mkdir('dataset/photo_imgs_npy')
os.mkdir('dataset/cartoon_imgs_npy')
os.mkdir('dataset/smooth_cartoon_imgs_npy')
# start preprocessing
preprocess('dataset/photo_imgs')
preprocess('dataset/cartoon_imgs')
preprocess('dataset/smooth_cartoon_imgs')