diff --git a/filter.py b/filter.py index 4150df2..7493e93 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,25 @@ from PIL import Image import numpy as np -img = Image.open("img2.jpg") -arr = np.array(img) -a = len(arr) -a1 = len(arr[1]) -i = 0 -while i < a - 11: - j = 0 - while j < a1 - 11: - s = 0 - for n in range(i, i + 10): - for n1 in range(j, j + 10): - n1 = arr[n][n1][0] - n2 = arr[n][n1][1] - n3 = arr[n][n1][2] - M = n1 + n2 + n3 - s += M - s = int(s // 100) - for n in range(i, i + 10): - for n1 in range(j, j + 10): - arr[n][n1][0] = int(s // 50) * 50 - arr[n][n1][1] = int(s // 50) * 50 - arr[n][n1][2] = int(s // 50) * 50 - j = j + 10 - i = i + 10 -res = Image.fromarray(arr) -res.save('res.jpg') + +def make_mosaic(array, grayscale:int, size_mosaic:int, row_length:int, column_length:int): + array = array[0: row_length // size_mosaic * size_mosaic, 0: column_length // size_mosaic * size_mosaic] + row_index = 0 + while row_index < row_length - (size_mosaic - 1): + column_index = 0 + while column_index < column_length - (size_mosaic - 1): + color = 0 + for step_row in range(row_index, row_index + size_mosaic): + color += sum(map(lambda step_column: sum(map(int, array[step_row][step_column])) // 3, + range(column_index, column_index + size_mosaic))) + color = ((color // size_mosaic**2) // grayscale) * grayscale + for step_row in range(row_index, row_index + size_mosaic): + for step_column in range(column_index, column_index + size_mosaic): + array[step_row][step_column] = np.full(3, color) + column_index += size_mosaic + row_index += size_mosaic + return array + +img_array = np.array(Image.open(input("Полное имя файла: "))) +Image.fromarray(make_mosaic(img_array, int(input("Градации серого: ")), + int(input("Размер мозайки: ")), len(img_array), + len(img_array[1]))).save('res.jpg') diff --git a/res.jpg b/res.jpg index d8d97ff..7183d91 100644 Binary files a/res.jpg and b/res.jpg differ