-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathfilter.py
More file actions
30 lines (21 loc) · 1.16 KB
/
filter.py
File metadata and controls
30 lines (21 loc) · 1.16 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
from PIL import Image
import numpy as np
def convert_image_to_mosaic(image, size, gradation_step):
for x in range(0, len(image), size):
for y in range(0, len(image[0]), size):
image[x:x + size, y:y + size] = get_average_brightness(
image[x:x + size, y:y + size], size, gradation_step)
return image
def get_average_brightness(block, size, gradation_step):
average_color = (block[:size, :size].sum() / 3) // size ** 2
return int(average_color // gradation_step) * gradation_step
def main():
image_file = Image.open(input("Введите имя файла, которое хотите конвертировать: "))
block_size = int(input("Введите размер блока: "))
gradations_count = int(input("Введите количество градаций серого: "))
image = np.array(image_file)
gradation_step = 255 // gradations_count
res = Image.fromarray(convert_image_to_mosaic(image, block_size, gradation_step))
res.save(input("Введите имя файла, в которой хотите сохранить результат: "))
if __name__ == '__main__':
main()