diff --git a/filter.py b/filter.py index f7fa90e..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 - 11: - j = 0 - while j < a1 - 11: - 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 - 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')