-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpad.py
141 lines (108 loc) · 3.52 KB
/
pad.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
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
###############################################################################
# pad.py
#
# sheneman@uidaho.edu
#
#
# The UNET CNN model requires all input images to be a consistent 256x256
# dimension. This tool preprocesses images of smaller dimensions to conform
# to that requirement.
#
# pad.py takes a set of grayscale input images and pad them evenly on top,
# bottom, and sides with negative pixels (value = 0) to form a 256x256 image
# with the original image properly centered.
#
# The related unpad.py tool performs the exact opposite operation in order to
# result in a file with its original dimensions.
#
# Usage:
# python pad.py [ --help | --verbose | --config=<YAML config file> ]
#
###############################################################################
import sys, os, getopt, yaml
from os import listdir
from PIL import Image, ImageOps
# for now, this is hardcoded, should put in YAML config file
PAD_SIZE = 256
###############################################################################
#
# HANDLE Command line arguments
#
#
def usage():
print("python pad.py [ --help | --verbose | --config=<YAML config filename> ] ")
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "config="])
except getopt.GetoptError as err:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
configfile = None
verbose = False
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-c", "--config"):
configfile = a
else:
assert False, "unhandled option"
if(configfile == None):
configfile="pad.yaml"
###############################################################################
###############################################################################
#
# Format and Example config YAML file:
#
# FORMAT:
# -------
# rawdir: <path to input raw images>
# outdir: <path to output directory for padded images>
#
# EXAMPLE:
# --------
# rawdir: "../images/raw"
# outdir: "../images/padded"
#
#################################################################################
cf = open(configfile, "r")
config = yaml.load(cf, Loader=yaml.FullLoader)
print("YAML CONFIG:")
for c in config:
print(" [%s]:\"%s\"" %(c,config[c]))
print("\n")
cf.close()
###############################################################################
## FUNCTION DEFINITIONS
###############################################################################
###############################################################################
#
# function: pad_file()
#
def pad_file(filename,new_width,new_height):
print("In pad_file(): file = %s" %filename)
fullpath = config["rawdir"] + '/' + filename
img = Image.open(fullpath)
(width,height) = img.size
if(width > PAD_SIZE or height > PAD_SIZE):
return(-1)
delta_w = new_width - width
delta_h = new_height - height
padding = (delta_w//2, delta_h//2, delta_w-(delta_w//2), delta_h-(delta_h//2))
new_img = ImageOps.expand(img, padding)
return(new_img)
###############################################################################
#
# read all files in the input directory and call pad_file() to pad them to
# PAD_SIZE. Output the padded image to the specified output directory
filenames = listdir(config["rawdir"])
for filename in filenames:
padded_image = pad_file(filename,PAD_SIZE,PAD_SIZE)
if(padded_image == -1):
print("skipping %s" %filename)
else:
savepath = config["outdir"] + '/' + filename
padded_image.save(savepath, "TIFF")
exit(0)