From 97d5494aa380781ad1ce5ef2954dc4515b8f4160 Mon Sep 17 00:00:00 2001 From: smileman0001 <111222333artem8@gmail.com> Date: Tue, 16 Nov 2021 14:43:24 +0500 Subject: [PATCH 1/4] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/filter.py b/filter.py index 4150df2..8f04663 100644 --- a/filter.py +++ b/filter.py @@ -1,27 +1,28 @@ from PIL import Image import numpy as np +np.seterr(over='ignore') 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] + for x in range(i, i + 10): + for y in range(j, j + 10): + n1 = arr[x][y][0] / 3 + n2 = arr[x][y][1] / 3 + n3 = arr[x][y][2] / 3 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 + for x in range(i, i + 10): + for y in range(j, j + 10): + arr[x][y][0] = int(s // 50) * 50 + arr[x][y][1] = int(s // 50) * 50 + arr[x][y][2] = int(s // 50) * 50 j = j + 10 i = i + 10 res = Image.fromarray(arr) From 6d337a186192f743dc31bfd41884708351ccaaff Mon Sep 17 00:00:00 2001 From: smileman0001 <111222333artem8@gmail.com> Date: Tue, 16 Nov 2021 18:20:46 +0500 Subject: [PATCH 2/4] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.py | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/filter.py b/filter.py index 8f04663..5fcb470 100644 --- a/filter.py +++ b/filter.py @@ -1,29 +1,40 @@ from PIL import Image import numpy as np + np.seterr(over='ignore') + +def find_average_brightness(x_idx, y_idx, size, array): + s = 0 + for x in range(x_idx, x_idx + size): + for y in range(y_idx, y_idx + size): + n1 = array[x][y][0] / 3 + n2 = array[x][y][1] / 3 + n3 = array[x][y][2] / 3 + M = n1 + n2 + n3 + s += M + return int(s // (size * size)) + + +def transform_to_mosaic(x_idx, y_idx, arr, size, step, average_brightness): + for x in range(x_idx, x_idx + size): + for y in range(y_idx, y_idx + size): + arr[x][y][0] = int(average_brightness // step) * step + arr[x][y][1] = int(average_brightness // step) * step + arr[x][y][2] = int(average_brightness // step) * step + + 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 x in range(i, i + 10): - for y in range(j, j + 10): - n1 = arr[x][y][0] / 3 - n2 = arr[x][y][1] / 3 - n3 = arr[x][y][2] / 3 - M = n1 + n2 + n3 - s += M - s = int(s // 100) - for x in range(i, i + 10): - for y in range(j, j + 10): - arr[x][y][0] = int(s // 50) * 50 - arr[x][y][1] = int(s // 50) * 50 - arr[x][y][2] = int(s // 50) * 50 - j = j + 10 - i = i + 10 + +size = int(input('Введите длину боковой стороны мозайки (например: 10 (x*x)) : ')) +gradation = int(input('Введите градацию серого (например: 5) : ')) +step = 255 // gradation + +width = len(arr) +height = len(arr[1]) + +for x in range(0, width - size + 1, size): + for y in range(0, height - size + 1, size): + transform_to_mosaic(x, y, arr, size, step, find_average_brightness(x, y, size, arr)) res = Image.fromarray(arr) res.save('res.jpg') From 808a6ad4ac92cb07477679ce49ddb86c709fd3b6 Mon Sep 17 00:00:00 2001 From: smileman0001 <111222333artem8@gmail.com> Date: Tue, 16 Nov 2021 18:26:29 +0500 Subject: [PATCH 3/4] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.py | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/filter.py b/filter.py index 5fcb470..37dc3e2 100644 --- a/filter.py +++ b/filter.py @@ -3,25 +3,10 @@ np.seterr(over='ignore') -def find_average_brightness(x_idx, y_idx, size, array): - s = 0 - for x in range(x_idx, x_idx + size): - for y in range(y_idx, y_idx + size): - n1 = array[x][y][0] / 3 - n2 = array[x][y][1] / 3 - n3 = array[x][y][2] / 3 - M = n1 + n2 + n3 - s += M - return int(s // (size * size)) - - -def transform_to_mosaic(x_idx, y_idx, arr, size, step, average_brightness): - for x in range(x_idx, x_idx + size): - for y in range(y_idx, y_idx + size): - arr[x][y][0] = int(average_brightness // step) * step - arr[x][y][1] = int(average_brightness // step) * step - arr[x][y][2] = int(average_brightness // step) * step +def transform_to_mosaic(x, y, arr, size, step): + average_brightness = np.mean(arr[x:x+size, y:y+size][:]) + arr[x:x+size, y:y+size][:] = int(average_brightness // step) * step img = Image.open("img2.jpg") arr = np.array(img) @@ -35,6 +20,6 @@ def transform_to_mosaic(x_idx, y_idx, arr, size, step, average_brightness): for x in range(0, width - size + 1, size): for y in range(0, height - size + 1, size): - transform_to_mosaic(x, y, arr, size, step, find_average_brightness(x, y, size, arr)) + transform_to_mosaic(x, y, arr, size, step) res = Image.fromarray(arr) res.save('res.jpg') From 6f518379c3f94b93c558b3f49a33a036a6bbb905 Mon Sep 17 00:00:00 2001 From: smileman0001 <111222333artem8@gmail.com> Date: Tue, 16 Nov 2021 18:30:34 +0500 Subject: [PATCH 4/4] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/filter.py b/filter.py index 37dc3e2..15753a6 100644 --- a/filter.py +++ b/filter.py @@ -8,7 +8,10 @@ def transform_to_mosaic(x, y, arr, size, step): average_brightness = np.mean(arr[x:x+size, y:y+size][:]) arr[x:x+size, y:y+size][:] = int(average_brightness // step) * step -img = Image.open("img2.jpg") +open_name = input('Введите назвние изображения : ') +save_name = input('Введите назвние выходного изображения : ') + +img = Image.open(open_name) arr = np.array(img) size = int(input('Введите длину боковой стороны мозайки (например: 10 (x*x)) : ')) @@ -22,4 +25,4 @@ def transform_to_mosaic(x, y, arr, size, step): for y in range(0, height - size + 1, size): transform_to_mosaic(x, y, arr, size, step) res = Image.fromarray(arr) -res.save('res.jpg') +res.save(save_name)