-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
53 lines (41 loc) · 1.62 KB
/
filter.py
File metadata and controls
53 lines (41 loc) · 1.62 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import doctest
from PIL import Image
import numpy as np
def get_gray(pix_width, pix_height, x, y):
"""
Вычисляет "шаг" серого цвета и возвращает среднюю яркость
>>> get_gray(10, 10, 10, 10)
19
>>> get_gray(10, 20, 10, 20)
20
"""
result = np.sum(pixels[pix_width: pix_width + x,pix_height: pix_height + y ]) / 3
result = int(result // (x * y))
return result
def replace_pixels(pix_width, x, pix_height, y, step_gray):
"""
Закрашивает каждый пиксель в один цвет средней яркости
"""
gray = get_gray(pix_width, pix_height, x, y)
pixels[pix_width: pix_width + x,pix_height: pix_height + y] = int(gray // step_gray) * step_gray
def get_gray_img(x, y, step_gray):
"""
Возвращает новое обработанное фильтром изображение
"""
height = len(pixels)
width = len(pixels[1])
pix_width = 0
while pix_width < height:
pix_height = 0
while pix_height < width:
replace_pixels(pix_width, x, pix_height, y, step_gray)
pix_height = pix_height + y
pix_width = pix_width + x
return pixels
if __name__ == '__main__':
doctest.testmod()
img = Image.open(input('Enter file name'))
pixels = np.array(img)
mozaik = input(('Enter width, height, gray step')).split()
name = input(('Enter name of the result file')) + '.jpg'
Image.fromarray(get_gray_img(int(mozaik[0]), int(mozaik[1]), int(mozaik[2]))).save(name)