From 004d02dca2c1a9185d4415d4547c5171542dc2d3 Mon Sep 17 00:00:00 2001 From: alyona_filyaeva Date: Sun, 14 Nov 2021 11:01:12 +0500 Subject: [PATCH 1/5] Task 1 --- filter.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/filter.py b/filter.py index 4150df2..12087f0 100644 --- a/filter.py +++ b/filter.py @@ -5,17 +5,17 @@ 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 + m1 = arr[n][n1][0] + m2 = arr[n][n1][1] + m3 = arr[n][n1][2] + M = m1 + m2 + m3 + s += M // 3 s = int(s // 100) for n in range(i, i + 10): for n1 in range(j, j + 10): From 5b8c8cca74448c34078f9e4fb207ff9819410ef1 Mon Sep 17 00:00:00 2001 From: alyona_filyaeva Date: Sun, 14 Nov 2021 11:33:12 +0500 Subject: [PATCH 2/5] Task 2 --- filter.py | 63 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/filter.py b/filter.py index 12087f0..18c64cc 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,41 @@ 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: - j = 0 - while j < a1: - s = 0 - for n in range(i, i + 10): - for n1 in range(j, j + 10): - m1 = arr[n][n1][0] - m2 = arr[n][n1][1] - m3 = arr[n][n1][2] - M = m1 + m2 + m3 - s += M // 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 get_gray(pix_width, pix_height, x, y): + result = 0 + for width in range(pix_width, pix_width + x): + for height in range(pix_height, pix_height + y): + color_1 = int(pixels[width][height][0]) + color_2 = int(pixels[width][height][1]) + color_3 = int(pixels[width][height][2]) + result += (color_1 + color_2 + color_3) / 3 + result = int(result // (x * y)) + return result + + +def replace_pixels(pix_width, x, pix_height, y, step_gray): + gray = get_gray(pix_width, pix_height, x, y) + for width in range(pix_width, pix_width + x): + for height in range(pix_height, pix_height + y): + pixels[width][height][0] = int(gray // step_gray) * step_gray + pixels[width][height][1] = int(gray // step_gray) * step_gray + pixels[width][height][2] = int(gray // step_gray) * step_gray + + +def get_gray_img(x, y, step_gray): + height = len(pixels) + width = len(pixels[1]) + pix_width = 0 + while pix_width < height: + pix_height = 0 + while pixels_y < width: + replace_pixels(pix_width, x, pix_height, y, step_gray) + pixels_y = pix_height + y + pix_width = pix_width + x + return pixels + +pixels = np.array(img) +res = Image.fromarray(get_gray_img(10, 10, 50)) +res.save('res.jpg') \ No newline at end of file From 44d56439eadf425cfd1553952cba573c923e3dc6 Mon Sep 17 00:00:00 2001 From: alyona_filyaeva Date: Sun, 14 Nov 2021 11:55:47 +0500 Subject: [PATCH 3/5] Task 4(without 3) --- filter.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/filter.py b/filter.py index 18c64cc..0dc5439 100644 --- a/filter.py +++ b/filter.py @@ -1,7 +1,7 @@ from PIL import Image import numpy as np -img = Image.open("img2.jpg") - +img = Image.open(input()) +pixels = np.array(img) def get_gray(pix_width, pix_height, x, y): result = 0 @@ -36,6 +36,6 @@ def get_gray_img(x, y, step_gray): pix_width = pix_width + x return pixels -pixels = np.array(img) -res = Image.fromarray(get_gray_img(10, 10, 50)) +mozaik = input(('Enter width, height, gray step')).split() +res = Image.fromarray(get_gray_img(int(mozaik[0]), int(mozaik[1]), int(mozaik[2]))) res.save('res.jpg') \ No newline at end of file From 8057dac796bd54b9ed9b6f61d6c0924ea0dbd819 Mon Sep 17 00:00:00 2001 From: alyona_filyaeva Date: Tue, 16 Nov 2021 09:47:52 +0500 Subject: [PATCH 4/5] Task 2 refactored --- filter.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/filter.py b/filter.py index 0dc5439..64b8edd 100644 --- a/filter.py +++ b/filter.py @@ -30,12 +30,13 @@ def get_gray_img(x, y, step_gray): pix_width = 0 while pix_width < height: pix_height = 0 - while pixels_y < width: + while pix_height < width: replace_pixels(pix_width, x, pix_height, y, step_gray) pixels_y = pix_height + y pix_width = pix_width + x return pixels mozaik = input(('Enter width, height, gray step')).split() -res = Image.fromarray(get_gray_img(int(mozaik[0]), int(mozaik[1]), int(mozaik[2]))) -res.save('res.jpg') \ No newline at end of file +name = input(('Enter name of the result file')) + '.jpg' + +Image.fromarray(get_gray_img(int(mozaik[0]), int(mozaik[1]), int(mozaik[2]))).save(name) From 96fbc2eeb4c465cdf77162ad83e068bfd1b6eb31 Mon Sep 17 00:00:00 2001 From: alyona_filyaeva Date: Tue, 16 Nov 2021 10:05:10 +0500 Subject: [PATCH 5/5] Task 3 --- filter.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/filter.py b/filter.py index 64b8edd..a59690b 100644 --- a/filter.py +++ b/filter.py @@ -4,24 +4,14 @@ pixels = np.array(img) def get_gray(pix_width, pix_height, x, y): - result = 0 - for width in range(pix_width, pix_width + x): - for height in range(pix_height, pix_height + y): - color_1 = int(pixels[width][height][0]) - color_2 = int(pixels[width][height][1]) - color_3 = int(pixels[width][height][2]) - result += (color_1 + color_2 + color_3) / 3 + result = np.sum(pixels[pix_width: pix_width + x,pix_height: pix_height + y ]) / 3 result = int(result // (x * y)) return result def replace_pixels(pix_width, x, pix_height, y, step_gray): gray = get_gray(pix_width, pix_height, x, y) - for width in range(pix_width, pix_width + x): - for height in range(pix_height, pix_height + y): - pixels[width][height][0] = int(gray // step_gray) * step_gray - pixels[width][height][1] = int(gray // step_gray) * step_gray - pixels[width][height][2] = int(gray // step_gray) * step_gray + pixels[pix_width: pix_width + x,pix_height: pix_height + y ] = int(gray // step_gray) * step_gray def get_gray_img(x, y, step_gray):