From 74621c0592e5aa5450ea4b71475196b1cac393fd Mon Sep 17 00:00:00 2001 From: dariruna <84535069+dariruna@users.noreply.github.com> Date: Wed, 17 Nov 2021 16:26:57 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A0=D1=83=D1=85=D1=82=D0=B8=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=94=D0=B0=D1=80=D1=8C=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit этап 1 --- filter.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/filter.py b/filter.py index f7fa90e..fea60ff 100644 --- a/filter.py +++ b/filter.py @@ -5,16 +5,16 @@ 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 + red = int(arr[n][n1][0]) + green = int(arr[n][n1][1]) + blue = int(arr[n][n1][2]) + M = (red + green + blue) // 3 s += M s = int(s // 100) for n in range(i, i + 10): From 1098880f8978c0fe376240e8327fb609027fdead Mon Sep 17 00:00:00 2001 From: dariruna <84535069+dariruna@users.noreply.github.com> Date: Wed, 17 Nov 2021 20:59:32 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=D1=8D=D1=82=D0=B0=D0=BF=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit этап 2 --- filter.py | 56 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/filter.py b/filter.py index fea60ff..b123fd9 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,36 @@ from PIL import Image import numpy as np + +class PixelArt: + def __init__(self, pixel_array, pixel_size:int, graduation:int): + self.image = np.array(pixel_array) + self.size = pixel_size + self.graduation = graduation + self.height = len(self.image) + self.width = len(self.image[0]) + + def make_image_grey(self): + for pix_width in range(0, self.width, self.size): + for pix_height in range(0, self.height, self.size): + self.make_color_grey(color=self.make_medium_color(pix_width, pix_height), pix_width=pix_width, pix_height=pix_height) + return Image.fromarray(self.image) + + def make_medium_color(self, pix_width, pix_height): + color = 0 + for column in range(pix_width, pix_width + self.size): + for line in range(pix_height, pix_height + self.size): + color += sum([int(self.image[column][line][j]) for j in range(3)]) + return int(color / 3 // (self.size ** 2)) + + def make_color_grey(self, color, pix_width, pix_height): + for column in range(pix_width, pix_width + self.size): + for line in range(pix_height, pix_height + self.size): + self.image[column][line][0] = int(color // self.graduation) * self.graduation + self.image[column][line][1] = int(color // self.graduation) * self.graduation + self.image[column][line][2] = int(color // self.graduation) * self.graduation + 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): - red = int(arr[n][n1][0]) - green = int(arr[n][n1][1]) - blue = int(arr[n][n1][2]) - M = (red + green + blue) // 3 - 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 - j = j + 10 - i = i + 10 -res = Image.fromarray(arr) +graduation = 50 +pixel_size = 10 +res = PixelArt(pixel_array=img, pixel_size=pixel_size, graduation=graduation).make_image_grey() res.save('res.jpg')