forked from douxbb/refactoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
38 lines (37 loc) · 1.26 KB
/
filter.py
File metadata and controls
38 lines (37 loc) · 1.26 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
from PIL import Image
import numpy as np
correct_path = False
CELL_X_OFFSET = 10
CELL_Y_OFFSET = 10
GREY_GRADATION = 50
while not correct_path:
img_path = input("Enter path to image for filtering: ")
try:
img = Image.open(img_path)
correct_path = True
except:
print("Incorrect path/file format, please try again")
img_matrix = np.array(img)
len_x = len(img_matrix)
len_y = len(img_matrix[1])
cell_x = 0
while cell_x < len_x:
cell_y = 0
while cell_y < len_y:
sum_RGB = 0
sum_RGB += np.sum(img_matrix[cell_x:cell_x + CELL_X_OFFSET, cell_y:cell_y + CELL_Y_OFFSET]) // 3
sum_RGB = int(sum_RGB // 100)
grey_matrix = np.zeros((CELL_X_OFFSET, CELL_Y_OFFSET, 3))
grey_matrix[:] = int(sum_RGB // GREY_GRADATION) * GREY_GRADATION
img_matrix[cell_x:cell_x + CELL_X_OFFSET, cell_y:cell_y + CELL_Y_OFFSET] = grey_matrix
cell_y = cell_y + CELL_Y_OFFSET
cell_x = cell_x + CELL_X_OFFSET
res = Image.fromarray(img_matrix)
correct_path = False
while not correct_path:
res_path = input("Enter path to resulting image: ")
try:
res.save(res_path)
correct_path = True
except:
print("Incorrect path/file format, please try again")