diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4e34b9c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..60ea18f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/other.xml b/.idea/other.xml new file mode 100644 index 0000000..a708ec7 --- /dev/null +++ b/.idea/other.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/refactoring17.iml b/.idea/refactoring17.iml new file mode 100644 index 0000000..0cf4183 --- /dev/null +++ b/.idea/refactoring17.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index e71f2cd..17d3128 100644 --- a/README.md +++ b/README.md @@ -10,16 +10,16 @@ Иногда делают такие панно прямо на зданиях. -![Здание](https://github.com/bibilov/refactoring/blob/main/632.jpg) +![Здание](632.jpg) Вот я прогнал через фильтр свое фото: -![Фото с фильтром](https://github.com/bibilov/refactoring/blob/main/m0oLR8Tx0zRG8s3SZQlQLnF8bhcnGu6AwzRA5aqi.png_4_1.png) +![Фото с фильтром](m0oLR8Tx0zRG8s3SZQlQLnF8bhcnGu6AwzRA5aqi.png_4_1.png) Вот так примерно выглядит процесс сборки панно: -![Сборка](https://github.com/bibilov/refactoring/blob/main/compile.png) +![Сборка](compile.png) Естественно, появилось желание написать вручную такой фильтр. Он может понадобится для пиксель-арта, создания игр с анимацией а-ля ранний Mortal Kombat, японских кроссвордов или для вязки свитеров ближе к НГ. Для чтения-записи изображений используется библиотека `pillow`, для всех остальных манипуляций — `numpy`. @@ -61,11 +61,11 @@ res.save('res.jpg') В результате из такой картинки: -![Исходная каритинка](https://github.com/bibilov/refactoring/blob/main/img2.jpg) +![Исходная каритинка](img2.jpg) Получается такая: -![Результат](https://github.com/bibilov/refactoring/blob/main/res.jpg) +![Результат](res.jpg) Это не тот результат, на который я рассчитывал, и вам предстоит много поработать с моим кодом. diff --git a/filter.py b/filter.py index 4150df2..bb94328 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,38 @@ -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 - 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) -res.save('res.jpg') +from PIL import Image + + +class PixelArt: + def __init__(self, chunk_size, color_step, file_path, save_path): + self.save_path = save_path + self.file_path = file_path + self.chunk_size = chunk_size + self.color_step = color_step + self.array = np.array(Image.open(file_path)) + + def generate_art(self): + x_len = len(self.array) + y_len = len(self.array[1]) + for x in range(0, x_len, self.chunk_size): + for y in range(0, y_len, self.chunk_size): + chunk_size_x = self.chunk_size if x_len - x >= self.chunk_size else x_len - x + chunk_size_y = self.chunk_size if y_len - y >= self.chunk_size else y_len - y + chunk = self.get_chuck_color(x, y, chunk_size_x, chunk_size_y) + for m in range(x, x + chunk_size_x): + for m1 in range(y, y + chunk_size_y): + self.array[m][m1] = chunk + return self + + def get_chuck_color(self, x, y, chunk_size_x, chunk_size_y): + color = (np.sum(self.array[x: x + chunk_size_x, y: y + chunk_size_y]) / 3 / self.chunk_size ** 2) + return color - color % (255 / self.color_step) + + def save(self): + Image.fromarray(self.array).save(self.save_path) + + +if __name__ == '__main__': + PixelArt(int(input('Enter chunk size: ')), + int(input('Enter count of colors: ')), + input('Enter original file path: '), + input('Enter result file path: ')).generate_art().save() diff --git a/img2.jpg b/img2.jpg index f8c3ed5..b69acdd 100644 Binary files a/img2.jpg and b/img2.jpg differ diff --git a/res.jpg b/res.jpg index d8d97ff..963675a 100644 Binary files a/res.jpg and b/res.jpg differ