From c6e57fb80c489b219d5876e4f8a6e827211ae343 Mon Sep 17 00:00:00 2001 From: Andrew <70693527+vazik66@users.noreply.github.com> Date: Mon, 15 Nov 2021 12:27:41 +0500 Subject: [PATCH 1/3] Stage 1. Fixed errors --- filter.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/filter.py b/filter.py index 4150df2..5a156a4 100644 --- a/filter.py +++ b/filter.py @@ -1,21 +1,22 @@ 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: +while i < a: j = 0 - while j < a1 - 11: + while j < a1: 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 + for n0 in range(j, j + 10): + n1 = int(arr[n][n0][0]) + n2 = int(arr[n][n0][1]) + n3 = int(arr[n][n0][2]) + s += (n1 + n2 + n3) / 3 s = int(s // 100) for n in range(i, i + 10): for n1 in range(j, j + 10): From 73b489b3b66ff252a788613c7cf81b06b7a317eb Mon Sep 17 00:00:00 2001 From: Andrew <70693527+vazik66@users.noreply.github.com> Date: Mon, 15 Nov 2021 14:18:25 +0500 Subject: [PATCH 2/3] Stage 2. Base improvements 1. Extracted functions 2. Renamed variables 3. Easy to access mosaic size and grayscale step 4. PEP8 --- filter.py | 60 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/filter.py b/filter.py index 5a156a4..8dc6b5a 100644 --- a/filter.py +++ b/filter.py @@ -2,28 +2,38 @@ 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: - j = 0 - while j < a1: - s = 0 - for n in range(i, i + 10): - for n0 in range(j, j + 10): - n1 = int(arr[n][n0][0]) - n2 = int(arr[n][n0][1]) - n3 = int(arr[n][n0][2]) - s += (n1 + n2 + n3) / 3 - 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 main(): + img = Image.open("img2.jpg") + + img_data = np.array(img) + img_size = img_data.shape + mosaic_size = 10 + grayscale_step = 50 + + for x in range(0, img_size[0], mosaic_size): + for y in range(0, img_size[1], mosaic_size): + avg_brightness = get_avg_brightness(img_data, mosaic_size, x, y) + apply_grayscale(img_data, mosaic_size, grayscale_step, x, y, avg_brightness) + + result = Image.fromarray(img_data) + result.save("res.jpg") + + +def apply_grayscale(img_data, mosaic_size, grayscale_step, x, y, avg_brightness): + for x1 in range(x, x + mosaic_size): + for y1 in range(y, y + mosaic_size): + img_data[x1][y1][0] = img_data[x1][y1][1] = img_data[x1][y1][2] = ( + int(avg_brightness // grayscale_step) * grayscale_step + ) + + +def get_avg_brightness(img_data, mosaic_size, x, y): + avg_brightness = 0 + for x1 in range(x, x + mosaic_size): + for y1 in range(y, y + mosaic_size): + avg_brightness += int(sum(img_data[x1][y1])) // 3 + return int(avg_brightness // mosaic_size ** 2) + + +if __name__ == "__main__": + main() From 399107cc00a7b654d39e6b16970e04e2ca5ee77d Mon Sep 17 00:00:00 2001 From: Andrew <70693527+vazik66@users.noreply.github.com> Date: Mon, 15 Nov 2021 15:15:15 +0500 Subject: [PATCH 3/3] Stage 3 & 4. 1. Replaced cycles with matrix transformations 2. Now usable from terminal --- filter.py | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/filter.py b/filter.py index 8dc6b5a..03f0d4d 100644 --- a/filter.py +++ b/filter.py @@ -2,38 +2,36 @@ import numpy as np -def main(): - img = Image.open("img2.jpg") +def get_avg_brightness(img_data, mosaic_size, x, y): + return np.average(img_data[x : x + mosaic_size, y : y + mosaic_size]) + + +def save_image(data, file_name, path): + Image.fromarray(data).save(f'{file_name}.{path.split(".")[-1]}') - img_data = np.array(img) - img_size = img_data.shape - mosaic_size = 10 - grayscale_step = 50 +def create_mosaic(img_data, img_size, mosaic_size, grayscale_step): for x in range(0, img_size[0], mosaic_size): for y in range(0, img_size[1], mosaic_size): avg_brightness = get_avg_brightness(img_data, mosaic_size, x, y) - apply_grayscale(img_data, mosaic_size, grayscale_step, x, y, avg_brightness) + img_data[x : x + mosaic_size, y : y + mosaic_size] = avg_brightness - avg_brightness % grayscale_step + - result = Image.fromarray(img_data) - result.save("res.jpg") - - -def apply_grayscale(img_data, mosaic_size, grayscale_step, x, y, avg_brightness): - for x1 in range(x, x + mosaic_size): - for y1 in range(y, y + mosaic_size): - img_data[x1][y1][0] = img_data[x1][y1][1] = img_data[x1][y1][2] = ( - int(avg_brightness // grayscale_step) * grayscale_step - ) +def main(): + path = input("Enter image path: ") + output_name = input("Enter name of output file: ") + mosaic_size = int(input("Enter mosaic size: ")) + grayscale_step = int(input("Enter grayscale step: ")) + img_data = np.array(Image.open(path)) + img_size = img_data.shape -def get_avg_brightness(img_data, mosaic_size, x, y): - avg_brightness = 0 - for x1 in range(x, x + mosaic_size): - for y1 in range(y, y + mosaic_size): - avg_brightness += int(sum(img_data[x1][y1])) // 3 - return int(avg_brightness // mosaic_size ** 2) + create_mosaic(img_data, img_size, mosaic_size, grayscale_step) + save_image(img_data, output_name, path) if __name__ == "__main__": - main() + try: + main() + except KeyboardInterrupt: + quit()