Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/refactoring17.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -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)

Это не тот результат, на который я рассчитывал, и вам предстоит много поработать с моим кодом.

Expand Down
64 changes: 37 additions & 27 deletions filter.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file modified img2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.