-
Notifications
You must be signed in to change notification settings - Fork 0
/
smooth
executable file
·62 lines (44 loc) · 1.65 KB
/
smooth
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
#!/usr/bin/env python3
"""
Create a smoothed version of an image.
"""
# TODO: check if we can use these cool functions from the ImageOps module:
# autocontrast and equalize.
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter as fmt
import numpy as np
from PIL import Image, ImageFilter, ImageOps
from maps import check_if_exists
def main():
args = get_args()
img = Image.open(args.image).convert('L')
if args.invert:
img = ImageOps.invert(img)
im0 = np.asarray(img)
kernel = ImageFilter.Kernel((5, 5),
[1, 1, 1, 1, 1,
1, 2, 2, 2, 1,
1, 2, 8, 2, 1,
1, 2, 2, 2, 1,
1, 1, 1, 1, 1])
for i in range(args.intensity):
img = img.filter(kernel)
img = Image.fromarray(np.minimum(im0, np.asarray(img)))
output = args.output or '%s_smoothed.%s' % tuple(args.image.rsplit('.', 1))
if not args.overwrite:
check_if_exists(output)
print('Writing file %s ...' % output)
img.save(output)
def get_args():
"Return the parsed command line arguments"
parser = ArgumentParser(description=__doc__, formatter_class=fmt)
add = parser.add_argument # shortcut
add('image', help='image file with the map')
add('--output', default='',
help='output file (if empty, it is generated from the image file name)')
add('--overwrite', action='store_true',
help='do not check if the output file already exists')
add('--invert', action='store_true', help='invert the colors of the image')
add('--intensity', type=int, default=10, help='intensity of the smoothing')
return parser.parse_args()
if __name__ == '__main__':
main()