diff --git a/filter.py b/filter.py index 4150df2..841d972 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,28 @@ 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') + +imgURL = input("Укажите название исходного изображения >> ") +imgSave = input("Укажите как сохранить изображение >> ") +img = Image.open(imgURL) + +arrImages = np.array(img) +height = len(arrImages) +width = len(arrImages[0]) + +size = int(input("Введите размер мозайки >> ")) +gray = int(input("Введите режим градации серого >> ")) +stepGrey = 256 // (gray - 1) + +def sumColor(arr, size, x, y): + return np.average(arr[x: x + size, y:y+size]) + +def grayColor(arr, size, x, y, sum_color, stepGrey): + arr[x: x + size, y:y+size] = int(sum_color // stepGrey) * stepGrey # sum_color - sum_color % gray_step + +for x in range(0, width, size): + for y in range(0, height, size): + sum_color = sumColor(arrImages, size, x, y) + grayColor(arrImages, size, x, y, sum_color, stepGrey) + +res = Image.fromarray(arrImages) +res.save(imgSave) \ No newline at end of file