From e1176e677e687cf6c101d8e0ab98bdc0a9bb1ca9 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Tue, 9 Nov 2021 18:35:53 +0500 Subject: [PATCH 1/5] Step 1 --- 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 2636adcb6abf3f27cae1afd9b27ee65243cb0f5c Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Fri, 12 Nov 2021 23:03:19 +0500 Subject: [PATCH 2/5] Step 2 --- filter.py | 58 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/filter.py b/filter.py index dc05914..d869990 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,38 @@ from PIL import Image import numpy as np + + +def convert_image_to_pixelart(image, chunk_size=10, grey_gradation=5): + arr = np.array(image) + gradient = chunk_size * grey_gradation + width = len(arr) + height = len(arr[1]) + + for i in range(0, width - chunk_size + 1, chunk_size): + for j in range(0, height - chunk_size + 1, chunk_size): + grey = 0 + + for x in range(i, i + chunk_size): + for y in range(j, j + chunk_size): + red = int(arr[x][y][0]) + green = int(arr[x][y][1]) + blue = int(arr[x][y][2]) + light = red + green + blue + grey += light / 3 + + grey = int(grey // chunk_size ** 2) + + for x in range(i, i + chunk_size): + for y in range(j, j + chunk_size): + local_grey = int(grey // gradient) * gradient + + arr[x][y][0] = local_grey + arr[x][y][1] = local_grey + arr[x][y][2] = local_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 = Image.fromarray(convert_image_to_pixelart(img)) res.save('res.jpg') From 2df9222e901aa2159230333edcd04659284f5428 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Sat, 13 Nov 2021 01:13:43 +0500 Subject: [PATCH 3/5] Step 3 --- filter.py | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/filter.py b/filter.py index d869990..86caa8d 100644 --- a/filter.py +++ b/filter.py @@ -5,30 +5,13 @@ def convert_image_to_pixelart(image, chunk_size=10, grey_gradation=5): arr = np.array(image) gradient = chunk_size * grey_gradation - width = len(arr) - height = len(arr[1]) - for i in range(0, width - chunk_size + 1, chunk_size): - for j in range(0, height - chunk_size + 1, chunk_size): - grey = 0 + for x in range(0, arr.shape[0], chunk_size): + for y in range(0, arr.shape[1], chunk_size): + grey = sum(map(lambda n: int(n)/3, arr[x:x+chunk_size, y:y+chunk_size].flatten())) + grey = int(grey / chunk_size ** 2 // gradient) * gradient - for x in range(i, i + chunk_size): - for y in range(j, j + chunk_size): - red = int(arr[x][y][0]) - green = int(arr[x][y][1]) - blue = int(arr[x][y][2]) - light = red + green + blue - grey += light / 3 - - grey = int(grey // chunk_size ** 2) - - for x in range(i, i + chunk_size): - for y in range(j, j + chunk_size): - local_grey = int(grey // gradient) * gradient - - arr[x][y][0] = local_grey - arr[x][y][1] = local_grey - arr[x][y][2] = local_grey + arr[x:x+chunk_size, y:y+chunk_size] = grey return arr From 9da4860b45ba57a7e3aed4097de6e382019a777f Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Sat, 13 Nov 2021 01:31:27 +0500 Subject: [PATCH 4/5] Step 4 --- filter.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/filter.py b/filter.py index 86caa8d..3d0286f 100644 --- a/filter.py +++ b/filter.py @@ -16,6 +16,16 @@ def convert_image_to_pixelart(image, chunk_size=10, grey_gradation=5): return arr -img = Image.open("img2.jpg") -res = Image.fromarray(convert_image_to_pixelart(img)) +image_name = input("Введите название файла изображения:") + +mosaic = input("Введите размер мозайки(или нажмите Ввод для значения по-умолчанию = 10):") +mosaic = int(mosaic) if mosaic else 10 + +gradation = input("Введите шаг градации(или нажмите Ввод для значения по-умолчанию = 5):") +gradation = int(gradation) if gradation else 5 + +img = Image.open(image_name) +res = Image.fromarray(convert_image_to_pixelart(img, mosaic, gradation)) res.save('res.jpg') + +print("Получившееся изображение сохранено как res.jpg") From 68b22ab308aee1976f58340b73ff866fe90bc1e0 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Tue, 16 Nov 2021 19:16:06 +0500 Subject: [PATCH 5/5] Added algorithm fixes --- filter.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/filter.py b/filter.py index 3d0286f..2573968 100644 --- a/filter.py +++ b/filter.py @@ -4,12 +4,12 @@ def convert_image_to_pixelart(image, chunk_size=10, grey_gradation=5): arr = np.array(image) - gradient = chunk_size * grey_gradation + gradient = 255 // grey_gradation for x in range(0, arr.shape[0], chunk_size): for y in range(0, arr.shape[1], chunk_size): - grey = sum(map(lambda n: int(n)/3, arr[x:x+chunk_size, y:y+chunk_size].flatten())) - grey = int(grey / chunk_size ** 2 // gradient) * gradient + grey = arr[x:x+chunk_size, y:y+chunk_size].sum() / 3 + grey = grey / chunk_size ** 2 // gradient * gradient arr[x:x+chunk_size, y:y+chunk_size] = grey