-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.py
54 lines (40 loc) · 1.63 KB
/
filter.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
from PIL import Image
import numpy as np
def pixel_colors_sum(pixel):
n1 = pixel[0]
n2 = pixel[1]
n3 = pixel[2]
return int(n1) + int(n2) + int(n3)
def calculate_average_value(arr, blockSize,y, x):
result = 0
count = blockSize * blockSize
for row in range(y, y + blockSize):
for col in range(x, x + blockSize):
result += pixel_colors_sum(arr[row][col])
return result // count
def set_grey_color(arr, averageValue, blockSize, i, j, step):
greyValue = int(averageValue // step) * step // 3
for row in range(i, i + blockSize):
for col in range(j, j + blockSize):
arr[row][col][0] = greyValue
arr[row][col][1] = greyValue
arr[row][col][2] = greyValue
def transform_image(file_name, gradation, blockSize):
image = Image.open(file_name)
arr = np.array(image)
height = len(arr)
width = len(arr[1])
step = 255 // gradation
for i in range(0, height, blockSize):
for j in range(0, width, blockSize):
averageValue = calculate_average_value(arr, blockSize,i, j)
set_grey_color(arr, averageValue, blockSize, i, j, step)
return Image.fromarray(arr)
def Main():
file_name = input("Введите имя входного файла ")
result_name = input("Введите имя выходного файла ")
gradation = int(input("Введите градацию "))
blockSize = int(input("Введите размерами мозайки "))
res = transform_image(file_name, gradation, blockSize)
res.save(result_name)
Main()