-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprocessor.py
More file actions
58 lines (38 loc) · 1.63 KB
/
Copy pathprocessor.py
File metadata and controls
58 lines (38 loc) · 1.63 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
from pathlib import Path
from skimage import exposure
from skimage import io
import numpy as np
allowed_file_extensions = ['.png', '.jpg', '.jpeg']
def normalize_images(inputdirectory: str, outputdirectory: str):
inputpath = Path(inputdirectory)
outputpath = Path(outputdirectory)
inputfiles = []
for extension in allowed_file_extensions:
inputfiles.extend(list(inputpath.glob('**/*' + extension)))
for infile in inputfiles:
abs_file_path = Path.absolute(infile)
file_path = str(abs_file_path)
output_file_name = get_output_file_name(infile, inputpath, outputpath)
print('normalizing image: {} to: {}'.format(infile, output_file_name))
img_output = normalize_image(file_path)
ensure_dir_exists_for_file(output_file_name)
save_image(str(output_file_name), img_output)
def normalize_image(file_path: str) -> np.ndarray:
img = io.imread(file_path)
img_output = exposure.equalize_adapthist(img)
return img_output
def save_image(file_path: str, img: np.ndarray):
io.imsave(file_path, img)
def get_output_file_name(infile: Path, inputpath: Path, outputpath: Path) \
-> Path:
parent_dir = infile.parent
if not inputpath.samefile(Path(parent_dir)):
parent_dir_name = parent_dir.stem
output_file_name = outputpath.joinpath(parent_dir_name, infile.name)
else:
output_file_name = outputpath.joinpath(infile.name)
return output_file_name
def ensure_dir_exists_for_file(file_name: Path):
output_parent_dir = file_name.parent
if not Path.exists(output_parent_dir):
Path.mkdir(output_parent_dir)