forked from blunghino/understanding_the_amazon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaugment_data.py
More file actions
65 lines (52 loc) · 1.67 KB
/
augment_data.py
File metadata and controls
65 lines (52 loc) · 1.67 KB
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
from PIL import Image
from numpy.random import rand
from torch import np
# def generate_rotations(image):
# out = []
# im1 = image.rotate(90)
# im2 = image.rotate(180)
# im3 = image.rotate(270)
# out.append(im1)
# out.append(im2)
# out.append(im3)
# out.append(image.transpose(Image.FLIP_LEFT_RIGHT))
# out.append(im1.transpose(Image.FLIP_LEFT_RIGHT))
# out.append(im2.transpose(Image.FLIP_LEFT_RIGHT))
# out.append(im3.transpose(Image.FLIP_LEFT_RIGHT))
# return out
def random_flip_rotation_pil(PIL_image):
## Returns a random transformation (flip or rotation) of the image
rando = rand()
if rando < 0.25:
out_image = PIL_image
elif rando < 0.5:
out_image = PIL_image.rotate(90)
elif rando < 0.75:
out_image = PIL_image.rotate(180)
else:
out_image = PIL_image.rotate(270)
rando_2 = rand()
if rando_2 < 0.5:
out_image.transpose(Image.FLIP_LEFT_RIGHT)
return out_image
def random_flip_rotation_np(a):
## Returns a random transformation (flip or rotation) of the image
rando = rand()
if rando < 0.25:
out_image = a
elif rando < 0.5:
out_image = np.rot90(a, k=1)
elif rando < 0.75:
out_image = np.rot90(a, k=2)
else:
out_image = np.rot90(a, k=3)
rando_2 = rand()
if rando_2 < 0.5:
out_image = np.fliplr(out_image)
return out_image
# if __name__ == '__main__':
# original_image = Image.open("train_1.jpg")
# out = random_flip_rotation(original_image)
# out.save('test.jpg')
# # for i, image in enumerate(generate_rotations(original_image)):
# image.save('test' + str(i) + '.jpg')