-
Notifications
You must be signed in to change notification settings - Fork 0
/
noiseate.py
67 lines (54 loc) · 2.3 KB
/
noiseate.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
import os
import random
from scipy import ndarray
# image processing library
import skimage as sk
from skimage import transform
from skimage import util
from skimage import io
import argparse
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-n", "--numimages", type=int, default=200,
help="num of generated noisy images")
ap.add_argument("-d", "--namefolder", type=str, required=True,
help="name of folder")
args = vars(ap.parse_args())
def random_rotation(image_array: ndarray):
# pick a random degree of rotation between 25% on the left and 25% on the right
random_degree = random.uniform(-25, 25)
return sk.transform.rotate(image_array, random_degree)
def random_noise(image_array: ndarray):
# add random noise to the image
return sk.util.random_noise(image_array)
def horizontal_flip(image_array: ndarray):
# horizontal flip doesn't need skimage, it's easy as flipping the image array of pixels !
return image_array[:, ::-1]
# dictionary of the transformations we defined earlier
available_transformations = {
'noise': random_noise,
}
folder_path = args["namefolder"]
num_files_desired = args["numimages"]
# find all files paths from the folder
images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
num_generated_files = 0
while num_generated_files <= num_files_desired:
# random image from the folder
image_path = random.choice(images)
# read image as an two dimensional array of pixels
image_to_transform = sk.io.imread(image_path)
# random num of transformation to apply
num_transformations_to_apply = random.randint(1, len(available_transformations))
num_transformations = 0
transformed_image = None
while num_transformations <= num_transformations_to_apply:
# random transformation to apply for a single image
key = random.choice(list(available_transformations))
transformed_image = available_transformations[key](image_to_transform)
num_transformations += 1
new_file_path = '%s/%s/%s.png' % (folder_path, folder_path, num_generated_files)
# write image to the disk
io.imsave(new_file_path, transformed_image)
num_generated_files += 1
print("Generated files so far - ", num_generated_files)