-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaugment_images.py
More file actions
162 lines (126 loc) · 5.26 KB
/
augment_images.py
File metadata and controls
162 lines (126 loc) · 5.26 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import matplotlib
try:
matplotlib.use('TkAgg')
except:
pass
import imageio
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np
import os
from PIL import Image
def transparency_checker(image):
img = Image.open(image, 'r')
if img.mode == "P":
transparent = img.info.get("transparency", -1)
for _, index in img.getcolors():
if index == transparent:
return True
elif img.mode == "RGBA":
extrema = img.getextrema()
if extrema[3][0] < 255:
return True
return False
def bg_swap(poke_file,bg_file,comb_file):
"""
"""
pokemon = Image.open(poke_file, 'r')
background = Image.open(bg_file,'r')
background= background.resize(pokemon.size)
comb_img = Image.new('RGB', (pokemon.size), (0, 0, 0, 0))
comb_img.paste(background, (0,0))
comb_img.paste(pokemon, (0,0), mask=pokemon)
final = comb_img.convert('RGB')
final.save(comb_file, format="jpeg")
def augment_image(image_loc,aug_loc,N,show=True,sub_dirs=None):
"""
image_loc = path to orginal image
aug_loc = path to augmented images
N = number of augmented images generated
"""
##########################################################
# check if image transparent
##########################################################
if transparency_checker(image_loc) == True:
print( ' - TRANSPARENT')
if not os.path.exists(bg_folder):
print('cant locate backgrounds')
for dirpath, dirs, files in os.walk(bg_folder):
for filename in files:
background = os.path.join(dirpath,filename)
new_combined_filename = os.path.join(comb_folder,image_loc[len(source_loc):])
if not os.path.exists(os.path.join(comb_folder,sub_dirs)):
os.makedirs(os.path.join(comb_folder,sub_dirs))
bg_swap(image_loc,background,new_combined_filename)
augment_image(new_combined_filename,aug_loc,N,show=False,sub_dirs=sub_dirs)
#########################
# get image
#########################
image = imageio.imread(image_loc, as_gray=False, pilmode="RGB")
if show==True:
print("Original:")
ia.imshow(image)
###########################
# augment
###########################
seq = iaa.Sequential([
iaa.Affine(rotate=(-25, 25)),
iaa.AdditiveGaussianNoise(scale=(30, 90)),
iaa.Crop(percent=(0, 0.4)),
iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge"), # crop and pad images
iaa.ElasticTransformation(alpha=90, sigma=9), # water-like effect
iaa.Cutout() # replace one squared area within the image by a constant intensity value
], random_order=True)
images_aug = [seq(image=image) for _ in range(N)]
if show==True:
print("Augmented:")
ia.imshow(ia.draw_grid(images_aug[:8], cols=4, rows=2),)
###########################
# save augmented images
###########################
if not os.path.exists(aug_loc):
os.makedirs(aug_loc)
for n in range(0,len(images_aug)):
new_image_name = "{}/{}-A{}.jpg".format(aug_loc,(image_loc.split('/')[-1]).split('.')[0],n)
imageio.imwrite(new_image_name, images_aug[n])
print(" [] - augmented images saved to '{}'".format(new_image_name))
#############################
def augment_folder(folder_loc,aug_loc,N):
"""
source_loc = path to orginal folder containing images to be augmented
aug_loc = path to augmented images
N = number of augmented images generated per orginal image
"""
for dirpath, dirs, files in os.walk(source_loc):
#sprint(dirs)
for filename in files:
#print(filename)
sub_dirs = dirpath[len(source_loc):]
if filename[0] == '.':
print(' # image error - "{}"'.format(os.path.join(sub_dirs,filename)))
continue
image_loc = os.path.join(dirpath,filename)
aug_loc_ = os.path.join(aug_loc,sub_dirs)
#print(image_loc)
#try:
augment_image(image_loc,aug_loc_,N,show=False,sub_dirs=sub_dirs)
#except:
# print(' # image error - "{}"'.format(os.path.join(dirpath[len(source_loc):],filename)))
# pass
##############################################################
# SETUP
##############################################################
print('starting...')
# source dir containg images to be augmented
path_to_training_data = 'Data/Images/'
source_loc = path_to_training_data+'GenX/'
# dir for augmented images to be placed (file structure will be kept i.e augmented/bublsaur/image,jpg)
aug_loc = path_to_training_data+'GenX_Augmented/'
# dir containg bg images for transparent pokemon
bg_folder = path_to_training_data+'backgrounds/'
# dir for combined transparent images with new backgrrounds
comb_folder = path_to_training_data+'GenX_BGswap/'
#temporarily set number of augmentations per image to 1 to test npy data creating
N=1
augment_folder(source_loc,aug_loc,N)
print('finished.')