From 6489399d30dcabe4cacebaab6d786be83f14358c Mon Sep 17 00:00:00 2001 From: senelus <83028541+senelus@users.noreply.github.com> Date: Wed, 17 Nov 2021 21:21:04 +0500 Subject: [PATCH 1/6] Update filter.py --- filter.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/filter.py b/filter.py index 4150df2..a88cea7 100644 --- a/filter.py +++ b/filter.py @@ -5,18 +5,18 @@ 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] - M = n1 + n2 + n3 + for k in range(j, j + 10): + n1 = arr[n][k][0] + n2 = arr[n][k][1] + n3 = arr[n][k][2] + M = int(n1) + int(n2) + int(n3) s += M - s = int(s // 100) + s = int(s / 3 // 100) for n in range(i, i + 10): for n1 in range(j, j + 10): arr[n][n1][0] = int(s // 50) * 50 From df490b9efb559e1eaadee74dbfd1b661a8f36cc4 Mon Sep 17 00:00:00 2001 From: senelus <83028541+senelus@users.noreply.github.com> Date: Wed, 17 Nov 2021 21:48:20 +0500 Subject: [PATCH 2/6] =?UTF-8?q?[2]=20=D0=AD=D1=82=D0=B0=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.py | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/filter.py b/filter.py index a88cea7..60bc68b 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,28 @@ 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 - 9: - j = 0 - while j < a1 - 9: - s = 0 - for n in range(i, i + 10): - for k in range(j, j + 10): - n1 = arr[n][k][0] - n2 = arr[n][k][1] - n3 = arr[n][k][2] - M = int(n1) + int(n2) + int(n3) - s += M - s = int(s / 3 // 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') +input_image = Image.open("img2.jpg") +image_array = np.array(input_image) +width = len(image_array) +height = len(image_array[1]) +current_width = 0 +while current_width < width - 9: + current_height = 0 + while current_height < height - 9: + total_brightness = 0 + for n in range(current_width, current_width + 10): + for k in range(current_height, current_height + 10): + first_component = image_array[n][k][0] + second_component = image_array[n][k][1] + third_component = image_array[n][k][2] + local_brightness = int(first_component) + int(second_component) + int(third_component) + total_brightness += local_brightness + total_brightness = int(total_brightness / 3 // 100) + for n in range(current_width, current_width + 10): + for first_component in range(current_height, current_height + 10): + image_array[n][first_component][0] = int(total_brightness // 50) * 50 + image_array[n][first_component][1] = int(total_brightness // 50) * 50 + image_array[n][first_component][2] = int(total_brightness // 50) * 50 + current_height = current_height + 10 + current_width = current_width + 10 +mosaic_image = Image.fromarray(image_array) +mosaic_image.save('res.jpg') From cef54d7b8583900b643b6a82bdeefc5ea3754bff Mon Sep 17 00:00:00 2001 From: senelus <83028541+senelus@users.noreply.github.com> Date: Wed, 17 Nov 2021 23:22:03 +0500 Subject: [PATCH 3/6] Update filter.py --- filter.py | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/filter.py b/filter.py index 60bc68b..2169bdb 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,27 @@ from PIL import Image import numpy as np + +def get_average_color(image_array, width, mosaic_width, height, mosaic_height): + mosaic_resolution = mosaic_height * mosaic_width + average_color_on_screen = np.sum(image_array[width: width + mosaic_width, height: height + mosaic_height]) // 3 + average_color = average_color_on_screen // mosaic_resolution + return average_color + +def get_gray_mosaic_array(image_array, mosaic_width, mosaic_height, scale): + width = len(image_array) + height = len(image_array[1]) + for current_width in range(0, width , mosaic_width): + for current_height in range(0, height , mosaic_height): + average_color = get_average_color(image_array, current_width, mosaic_width , current_height, mosaic_height) + gray_color = int(average_color // scale) * scale + image_array[current_width: current_width + mosaic_width, current_height: current_height + mosaic_height] = np.full(3, gray_color) + return image_array + +mosaic_width = 10 +mosaic_height = 10 +scale = 50 input_image = Image.open("img2.jpg") image_array = np.array(input_image) -width = len(image_array) -height = len(image_array[1]) -current_width = 0 -while current_width < width - 9: - current_height = 0 - while current_height < height - 9: - total_brightness = 0 - for n in range(current_width, current_width + 10): - for k in range(current_height, current_height + 10): - first_component = image_array[n][k][0] - second_component = image_array[n][k][1] - third_component = image_array[n][k][2] - local_brightness = int(first_component) + int(second_component) + int(third_component) - total_brightness += local_brightness - total_brightness = int(total_brightness / 3 // 100) - for n in range(current_width, current_width + 10): - for first_component in range(current_height, current_height + 10): - image_array[n][first_component][0] = int(total_brightness // 50) * 50 - image_array[n][first_component][1] = int(total_brightness // 50) * 50 - image_array[n][first_component][2] = int(total_brightness // 50) * 50 - current_height = current_height + 10 - current_width = current_width + 10 -mosaic_image = Image.fromarray(image_array) +mosaic_array = get_gray_mosaic_array(image_array, mosaic_width, mosaic_height, scale) +mosaic_image = Image.fromarray(mosaic_array) mosaic_image.save('res.jpg') From 2b8eb37638dfbda6109245b8d6010ecc19d7170f Mon Sep 17 00:00:00 2001 From: senelus <83028541+senelus@users.noreply.github.com> Date: Wed, 17 Nov 2021 23:22:42 +0500 Subject: [PATCH 4/6] =?UTF-8?q?[3]=20=D0=AD=D1=82=D0=B0=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From 83096e2d9111a47367ca5cb6e5d697a6bd425737 Mon Sep 17 00:00:00 2001 From: senelus <83028541+senelus@users.noreply.github.com> Date: Wed, 17 Nov 2021 23:39:29 +0500 Subject: [PATCH 5/6] =?UTF-8?q?[3]=20=D0=AD=D1=82=D0=B0=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/filter.py b/filter.py index 2169bdb..a1684b4 100644 --- a/filter.py +++ b/filter.py @@ -13,13 +13,13 @@ def get_gray_mosaic_array(image_array, mosaic_width, mosaic_height, scale): for current_width in range(0, width , mosaic_width): for current_height in range(0, height , mosaic_height): average_color = get_average_color(image_array, current_width, mosaic_width , current_height, mosaic_height) - gray_color = int(average_color // scale) * scale + gray_color = average_color - average_color % scale image_array[current_width: current_width + mosaic_width, current_height: current_height + mosaic_height] = np.full(3, gray_color) return image_array -mosaic_width = 10 -mosaic_height = 10 -scale = 50 +mosaic_width = int(input("Введите целое положительное число для ширины мозайки: ")) +mosaic_height = int(input("Введите целое положительное число для высоты мозайки: ")) +scale = int(input("Введите целое положительное число для масштаба градации серых цветов в мозайке: ")) input_image = Image.open("img2.jpg") image_array = np.array(input_image) mosaic_array = get_gray_mosaic_array(image_array, mosaic_width, mosaic_height, scale) From 1b3444720e75dbb8567dce8858f8d23294ea7956 Mon Sep 17 00:00:00 2001 From: senelus <83028541+senelus@users.noreply.github.com> Date: Wed, 17 Nov 2021 23:50:24 +0500 Subject: [PATCH 6/6] =?UTF-8?q?[4]=20=D0=AD=D1=82=D0=B0=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/filter.py b/filter.py index a1684b4..457f02a 100644 --- a/filter.py +++ b/filter.py @@ -1,12 +1,12 @@ from PIL import Image import numpy as np -def get_average_color(image_array, width, mosaic_width, height, mosaic_height): - mosaic_resolution = mosaic_height * mosaic_width - average_color_on_screen = np.sum(image_array[width: width + mosaic_width, height: height + mosaic_height]) // 3 - average_color = average_color_on_screen // mosaic_resolution - return average_color - +def transform_image_to_mosaic(input_image, mosaic_width, mosaic_height, scale, mosaic_name, format_name): + image_array = np.array(input_image) + mosaic_array = get_gray_mosaic_array(image_array, mosaic_width, mosaic_height, scale) + mosaic_image = Image.fromarray(mosaic_array) + mosaic_image.save(mosaic_name + format_name) + def get_gray_mosaic_array(image_array, mosaic_width, mosaic_height, scale): width = len(image_array) height = len(image_array[1]) @@ -16,12 +16,17 @@ def get_gray_mosaic_array(image_array, mosaic_width, mosaic_height, scale): gray_color = average_color - average_color % scale image_array[current_width: current_width + mosaic_width, current_height: current_height + mosaic_height] = np.full(3, gray_color) return image_array + +def get_average_color(image_array, width, mosaic_width, height, mosaic_height): + mosaic_resolution = mosaic_height * mosaic_width + average_color_on_screen = np.sum(image_array[width: width + mosaic_width, height: height + mosaic_height]) // 3 + average_color = average_color_on_screen // mosaic_resolution + return average_color +input_image = Image.open(input("Введите путь до изображение: ")) mosaic_width = int(input("Введите целое положительное число для ширины мозайки: ")) mosaic_height = int(input("Введите целое положительное число для высоты мозайки: ")) scale = int(input("Введите целое положительное число для масштаба градации серых цветов в мозайке: ")) -input_image = Image.open("img2.jpg") -image_array = np.array(input_image) -mosaic_array = get_gray_mosaic_array(image_array, mosaic_width, mosaic_height, scale) -mosaic_image = Image.fromarray(mosaic_array) -mosaic_image.save('res.jpg') +mosaic_name = input("Введите название для готовой мозайки: ") +format_name = input("Введите формат для готовой мозайки: ") +transform_image_to_mosaic(input_image, mosaic_width, mosaic_height, scale, mosaic_name, format_name)