-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr05.py
71 lines (63 loc) · 2.11 KB
/
pr05.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
import numpy as np
from PIL import Image
# Local modules
import filters
import utils
MAX_QUALITY = 95
COLOR_CHANNELS = 3
MAX_COLOR = 255
IMAGES_DIR = "images"
IMG_FILENAME = f"{IMAGES_DIR}/mickey.jpg"
OUTPUT_BOX_BLUR = "blur.jpg"
OUTPUT_MOTION_BLUR = "motion.jpg"
def main():
while True:
opt = input(
"Enter an option:\n"
"[1] for box blur\n"
"[2] for motion blur\n"
"[3] for dilate\n"
"[4] for erode\n"
"[5] for edge\n"
"[0] to quit\n"
)
if opt == '0':
quit()
img = Image.open(IMG_FILENAME)
img_arr = np.array(img)
h, w, _ = img_arr.shape
# output_arr = np.zeros((h, w, COLOR_CHANNELS))
timer = utils.Timer()
timer.start()
if opt == '1':
print("Creating box blur...")
output_filename = OUTPUT_BOX_BLUR
kernel = filters.box_blur_kernel()
output_arr = filters.convolve(img_arr, kernel)
elif opt == '2':
print("Creating motion blur...")
output_filename = OUTPUT_MOTION_BLUR
kernel = filters.motion_blur_kernel()
output_arr = filters.convolve(img_arr, kernel)
elif opt == '3':
print("Creating dilation...")
output_filename = "dilation.jpg"
img_arr = utils.open_image(f"{IMAGES_DIR}/circle.jpg")
output_arr = filters.dilate(img_arr)
elif opt == '4':
print("Creating erosion...")
output_filename = "erosion.jpg"
img_arr = utils.open_image(f"{IMAGES_DIR}/box.jpg")
output_arr = filters.erode(img_arr)
else:
print("Creating edge...")
output_filename = "edge.jpg"
grayscale = np.array(img.convert('L'))
output_arr = filters.edge(grayscale)
timer.stop()
print(f"Total time spent: {timer}")
output_img = Image.fromarray(output_arr)
output_img.save(output_filename, quality=MAX_QUALITY)
print(f"Image saved in {output_filename}")
if __name__ == '__main__':
main()