-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageTransformationKai.py
105 lines (91 loc) · 4.5 KB
/
ImageTransformationKai.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import argparse, os
import numpy as np
import scipy.misc
import skimage
from PIL import Image, ImageDraw
from Queue import Queue
from threading import Thread
def main(args):
input_img = [f for f in os.listdir(args.image_dir) if os.path.isfile(os.path.join(args.image_dir, f)) and (f.endswith('.jpg') or f.endswith('.png'))]
if not os.path.exists(args.output_image_dir):
os.makedirs(args.output_image_dir)
if args.max_images > 0:
input_img = input_img[:args.max_images]
q = Queue()
for i, f in enumerate(input_img):
q.put((i, f))
def worker():
alpha = args.const_alpha
beta = args.const_beta
while True:
i, f = q.get()
fname = os.path.splitext(f)[0]
if args.transformation < 3:
out_file = os.path.join(args.output_image_dir, fname + '.' + args.output_type)
if args.resize299 != 0:
img = scipy.misc.imread(os.path.join(args.image_dir, f)).astype(dtype=np.float32)
img = scipy.misc.imresize(img, (299, 299))
scipy.misc.imsave(out_file, img)
img = Image.open(out_file)
else:
img = Image.open(os.path.join(args.image_dir, f))
if args.transformation == 1:
ow, oh = img.size
if alpha >= 0:
xs = 0
xe = oh - alpha
else:
xs = -alpha
xe = oh
if beta >= 0:
ys = 0
ye = ow - beta
else:
ys = -beta
ye = ow
box = (ys, xs, ye, xe)
img2 = img.crop(box)
img = img.point(lambda i : 0)
img.paste(img2, (ys + beta, xs + alpha, ye + beta, xe + alpha))
elif args.transformation == 2:
img = img.rotate(float(alpha), expand=True)
img.save(out_file)
else:
img = scipy.misc.imread(os.path.join(args.image_dir, f)).astype(dtype=np.float32) / 255 * 2 - 1
times = beta
if args.transformation == 3:
for i in range(1, times + 1):
out_file = os.path.join(args.output_image_dir, fname + '_' + str(i) + '.' + args.output_type)
img2 = (skimage.util.random_noise(img, mode='gaussian', clip=True, var=(float(alpha) / 255 * 2) ** 2) + 1) / 2 * 255
scipy.misc.imsave(out_file, img2.astype(dtype=np.int))
elif args.transformation == 4:
for i in range(1, times + 1):
out_file = os.path.join(args.output_image_dir, fname + '_' + str(i) + '.' + args.output_type)
noise = np.random.uniform(-float(alpha) / 255, float(alpha)/255, size=img.shape)
img2 = ((img + noise) + 1) / 2 * 255
scipy.misc.imsave(out_file, img2.astype(dtype=np.int))
if i % 100 == 0:
print 'Writing image %d / %d' % (i, len(input_img))
q.task_done()
numw = min(q.qsize(), args.num_workers)
for i in xrange(numw):
t = Thread(target=worker)
t.daemon = True
t.start()
q.join()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
#Input Settings
parser.add_argument('--image_dir', help='Directory containing all images')
parser.add_argument('--transformation', default=0, help='0 = none, 1 = shift, 2 = rotation, 3 = random Gaussian, 4 = random Uniform', type=int)
parser.add_argument('--const_alpha', default=0, help='Degree for rotation, dx for shift, std dev for gaussian, +- range for random uniform', type=int)
parser.add_argument('--const_beta', default=0, help='dy for shift, number of trails for random', type=int)
#Output Settings
parser.add_argument('--output_image_dir', help='Directory to output transformed images')
parser.add_argument('--output_type', default='jpg', help='File type to output')
parser.add_argument('--resize299', default=0, help='Whether to resize the picture to 299 * 299')
#Option Settings
parser.add_argument('--num_workers', default=10, help='Number of threads', type=int)
parser.add_argument('--max_images', default=-1, help='Total number of images to process', type=int)
args = parser.parse_args()
main(args)