Skip to content
Open
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
59 changes: 33 additions & 26 deletions filter.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
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 count_gray(pixels, pixels_x, pixels_y, size):
gray = np.sum((pixels[pixels_x: pixels_x + size, pixels_y: pixels_y + size]) / 3)
return int(gray // (size * size))


def replace_pixels(pixels, pixels_x, pixels_y, size, step):
gray = count_gray(pixels, pixels_x, pixels_y, size)
pixels[pixels_x: pixels_x + size, pixels_y: pixels_y + size] = int(gray // step) * step


def convert_gray_img(pixels, size, grey_step):
step = 255 // (grey_step - 1)
height = len(pixels)
width = len(pixels[1])
pixels_x = 0
while pixels_x < height:
pixels_y = 0
while pixels_y < width:
replace_pixels(pixels, pixels_x, pixels_y, size, step)
pixels_y = pixels_y + size
pixels_x = pixels_x + size
return pixels


img = Image.open(input('Имя входного файла:'))
pixels_img = np.array(img)
size_pixel = int(input('Размер пикселя(блока):'))
step_gray = int(input('Количество градаций:'))
res_file = input('Имя для сохранения файла:')
res = Image.fromarray(convert_gray_img(pixels_img, size_pixel, step_gray))
res.save(res_file)