-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_with_filename.py
More file actions
32 lines (22 loc) · 1.03 KB
/
filter_with_filename.py
File metadata and controls
32 lines (22 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
from PIL import Image
import numpy as np
def apply_filter():
img = Image.open("cat test photo.jpg")
mosaic_size = 10
count_gradations = 5
data_img = np.array(img)
size_img = data_img.shape
for x in range(0, size_img[0], mosaic_size):
for y in range(0, size_img[1], mosaic_size):
paint_cell(data_img, x, y, mosaic_size, count_gradations)
res = Image.fromarray(data_img)
res.save('result_filter_with_filename.jpg')
def find_average_brightness_cell(arr, px_width, px_height, mosaic_size):
return np.average(arr[px_width:px_width + mosaic_size, px_height: px_height + mosaic_size])
def paint_cell(arr, px_width, px_height, mosaic_size, count_gradations):
average_brightness = find_average_brightness_cell(arr, px_width, px_height, mosaic_size)
gradations = 255 // count_gradations
px_brightness = average_brightness // gradations * gradations
arr[px_width:px_width + mosaic_size, px_height:px_height + mosaic_size, ][:] = px_brightness
if __name__ == "__main__":
apply_filter()