forked from douxbb/refactoring17
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter.py
More file actions
37 lines (30 loc) · 1.26 KB
/
filter.py
File metadata and controls
37 lines (30 loc) · 1.26 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
from PIL import Image
import numpy as np
import sys
def get_mosaic_image(image, mosaic_size=10, gradation=4):
threshold = 255 // gradation
image_tensor = np.array(image).astype(int)
image_length = len(image_tensor)
image_height = len(image_tensor[0])
i = 0
while i < image_length:
j = 0
while j < image_height:
sector = image_tensor[i: i + mosaic_size, j: j + mosaic_size]
colors_sum = np.sum(sector)
average_color = int(colors_sum // (mosaic_size ** 2))
set_color(int(average_color // threshold) * threshold / 3, image_tensor, mosaic_size, i, j)
j += mosaic_size
i += mosaic_size
return Image.fromarray(np.uint8(image_tensor))
def set_color(new_color, tensor, mosaic_size, i, j):
for sector_i in range(i, i + mosaic_size):
for sector_j in range(j, j + mosaic_size):
for c in range(3):
tensor[sector_i][sector_j][c] = new_color
if __name__ == "__main__":
source_name = sys.argv[1]
output_name = sys.argv[2]
source_img = Image.open(source_name)
image_tensor = np.array(source_img).astype(int)
get_mosaic_image(image_tensor, mosaic_size=10, gradation=5).save(output_name)