-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
36 lines (30 loc) · 1.03 KB
/
filter.py
File metadata and controls
36 lines (30 loc) · 1.03 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
from PIL import Image
import numpy as np
img = Image.open(input("Enter image you want to change: "))
result_img = input("Enter image to write your result onto: ")
arr = np.array(img)
height = len(arr)
width = len(arr[1])
size = int(input("Enter size: "))
gradation = int(input("Enter gradation: "))
step = int(255 / gradation)
def return_average(i, j, arr, size):
color_sum = np.sum((arr[i: i + size, j: j + size]) / 3)
average = int(color_sum // (size * size))
return average
class Grey:
def __init__(self, step, height, width, size, arr):
self.step = step
self.height = height
self.width = width
self.size = size
self.arr = arr
def getGrey(self):
for i in range(0, self.height, self.size):
for j in range(0, self.width, self.size):
average = return_average(i, j, arr, size)
self.arr[i: i + self.size, j: j + self.size] = int(average // self.step) * self.step
return self.arr
newPicture = Grey(step, height, width, size, arr)
res = Image.fromarray(newPicture.getGrey())
res.save(result_img)