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
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,34 @@

Естественно, появилось желание написать вручную такой фильтр. Он может понадобится для пиксель-арта, создания игр с анимацией а-ля ранний Mortal Kombat, японских кроссвордов или для вязки свитеров ближе к НГ. Для чтения-записи изображений используется библиотека `pillow`, для всех остальных манипуляций — `numpy`.


```python
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
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')
```
Expand Down
49 changes: 23 additions & 26 deletions filter.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
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')


def pixel_brightness(arr, b_s, gr, i, j):
brightness = np.sum(arr[i: i + b_s, j: j + b_s]) // (b_s * b_s * 3)
brightness -= brightness % gr
return brightness


def transform_to_mosaic(arr, b_s, gr):
for i in range(0, len(arr), b_s):
for j in range(0, len(arr[1]), b_s):
brightness = pixel_brightness(arr, b_s, gr, i, j)
arr[i: i + b_s, j: j + b_s] = np.full(3, brightness)


def convert_image_to_mosaic(img_in="img2.jpg", img_out="res.jpg", block_size=10, gradation_step=50):
img = Image.open(img_in)
img_arr = np.array(img)
transform_to_mosaic(img_arr, block_size, gradation_step)
res = Image.fromarray(img_arr)
res.save(img_out)

convert_image_to_mosaic()
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.
Binary file added tests/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 added tests/res.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 added tests/res2.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 added tests/res3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions tests/test_2step.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def convert_image(img_in="img2.jpg",

# после выполнения с аргументами по умолчанию метод должен возвращать изображение res.jpg
result = Image.open("res.jpg")
correct = Image.open("tests/out/1out.jpg") # сравниваем с верным изображением по умолчанию
correct = Image.open("outWin/1out.jpg") # сравниваем с верным изображением по умолчанию
# для тестирования на Windows - поменять путь на tests/outWin

diff = ImageChops.difference(correct, result)
Expand All @@ -37,7 +37,7 @@ def test_2_2():
# 255 // 63 = 4 градации серого

result = Image.open("res2.jpg")
correct = Image.open("tests/out/2out.jpg")
correct = Image.open("outWin/2out.jpg")

diff = ImageChops.difference(correct, result)
assert not diff.getbbox()
Expand All @@ -51,7 +51,12 @@ def test_2_3():
gradation_step=1)

result = Image.open("res3.jpg")
correct = Image.open("tests/out/3out.jpg") # для тестирования на Windows - поменять путь на tests/outWin
correct = Image.open("outWin/3out.jpg") # для тестирования на Windows - поменять путь на tests/outWin

diff = ImageChops.difference(correct, result)
assert not diff.getbbox()


test_2_1()
test_2_2()
test_2_3()