diff --git a/filter.py b/filter.py index 4150df2..056839e 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,33 @@ from PIL import Image import numpy as np -img = Image.open("img2.jpg") +img = Image.open(input("Enter image you want to change: ")) +result_img = input("Enter image to write your result onto: ") arr = np.array(img) -a = len(arr) -a1 = len(arr[1]) -i = 0 -while i < a - 11: - j = 0 - while j < a1 - 11: - s = 0 - for n in range(i, i + 10): - for n1 in range(j, j + 10): - n1 = arr[n][n1][0] - n2 = arr[n][n1][1] - n3 = arr[n][n1][2] - M = n1 + n2 + n3 - s += M - s = int(s // 100) - for n in range(i, i + 10): - for n1 in range(j, j + 10): - arr[n][n1][0] = int(s // 50) * 50 - arr[n][n1][1] = int(s // 50) * 50 - arr[n][n1][2] = int(s // 50) * 50 - j = j + 10 - i = i + 10 -res = Image.fromarray(arr) -res.save('res.jpg') +height = len(arr) +width = len(arr[1]) +size = int(input("Enter size: ")) +gradation = int(input("Enter gradation: ")) +step = int(255 / gradation) + +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): + i = 0 + while i < self.height: + j = 0 + while j < self.width: + color_sum = np.sum((self.arr[i: i + self.size, j: j + self.size]) / 3) + average = int(color_sum // (self.size*self.size)) + self.arr[i: i + self.size, j: j + self.size] = int(average // self.step) * self.step + j = j + self.size + i = i + self.size + return self.arr + +newPicture = Grey(step, height, width, size, arr) +res = Image.fromarray(newPicture.getGrey()) +res.save(result_img)