From 6a11462b25f8f3540a518b053b519404f7aa0683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?= Date: Sat, 20 Nov 2021 20:32:26 +0500 Subject: [PATCH 1/3] =?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 | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/filter.py b/filter.py index 4150df2..dc05914 100644 --- a/filter.py +++ b/filter.py @@ -5,23 +5,23 @@ a = len(arr) a1 = len(arr[1]) i = 0 -while i < a - 11: +while i < a - 9: j = 0 - while j < a1 - 11: + while j < a1 - 9: 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 y in range(j, j + 10): + n1 = int(arr[n][y][0]) + n2 = int(arr[n][y][1]) + n3 = int(arr[n][y][2]) M = n1 + n2 + n3 - s += M + 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 + for y in range(j, j + 10): + arr[n][y][0] = int(s // 50) * 50 + arr[n][y][1] = int(s // 50) * 50 + arr[n][y][2] = int(s // 50) * 50 j = j + 10 i = i + 10 res = Image.fromarray(arr) From 6454b605a699827ea04aec574fe3be9626674db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?= Date: Sat, 20 Nov 2021 20:45:02 +0500 Subject: [PATCH 2/3] =?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 | 56 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/filter.py b/filter.py index dc05914..f2b1c96 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,34 @@ from PIL import Image import numpy as np + + +def pixelizate(path_to_source, block_size=10, grey_value=5): + arr = np.array(path_to_source) + gradient = block_size * grey_value + width = len(arr) + height = len(arr[1]) + + for i in range(0, width - block_size + 1, block_size): + for j in range(0, height - block_size + 1, block_size): + grey = 0 + + for x in range(i, i + block_size): + for y in range(j, j + block_size): + light = np.sum(arr[x][y]) + grey += light / 3 + + grey = int(grey // block_size ** 2) + + for x in range(i, i + block_size): + for y in range(j, j + block_size): + block_grey = int(grey // gradient) * gradient + arr[x][y][0] = block_grey + arr[x][y][1] = block_grey + arr[x][y][2] = block_grey + + return arr + + img = Image.open("img2.jpg") -arr = np.array(img) -a = len(arr) -a1 = len(arr[1]) -i = 0 -while i < a - 9: - j = 0 - while j < a1 - 9: - s = 0 - for n in range(i, i + 10): - for y in range(j, j + 10): - n1 = int(arr[n][y][0]) - n2 = int(arr[n][y][1]) - n3 = int(arr[n][y][2]) - M = n1 + n2 + n3 - s += M / 3 - s = int(s // 100) - for n in range(i, i + 10): - for y in range(j, j + 10): - arr[n][y][0] = int(s // 50) * 50 - arr[n][y][1] = int(s // 50) * 50 - arr[n][y][2] = int(s // 50) * 50 - j = j + 10 - i = i + 10 -res = Image.fromarray(arr) -res.save('res.jpg') +res = Image.fromarray(pixelizate(img)) +res.save('res.jpg') \ No newline at end of file From 3ed08cec72d2db09af89287fac8ac97a2b3ad643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?= Date: Sat, 20 Nov 2021 20:50:13 +0500 Subject: [PATCH 3/3] =?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 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/filter.py b/filter.py index f2b1c96..d0c1307 100644 --- a/filter.py +++ b/filter.py @@ -29,6 +29,11 @@ def pixelizate(path_to_source, block_size=10, grey_value=5): return arr -img = Image.open("img2.jpg") +img_path = input('Введите путь до изображения:') +chunk_size = input('Введите размер блоков мозаики в пикселях (default 10):') +gray_scale = input('Введите шаг градации (default 5):') + +img = Image.open(img_path) res = Image.fromarray(pixelizate(img)) -res.save('res.jpg') \ No newline at end of file +res.save('res.jpg') +print('Сохранено в файл res.jpg')