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
55 changes: 29 additions & 26 deletions filter.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
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')
class Filter():
def __init__(self, img, mosaic_size, grayscale):
self.arr = np.array(img)
self.mosaic_size = mosaic_size
self.grayscale = grayscale

def __GetAverage(self, y, x):
average = np.sum((self.arr[y: y + self.mosaic_size, x: x + self.mosaic_size]))/3
return int(average // (self.mosaic_size*self.mosaic_size))

def __DoGray(self, y, x):
average = self.__GetAverage(y, x)
self.arr[y: y + self.mosaic_size, x: x + self.mosaic_size] = int(average // self.grayscale) * self.grayscale

def __SaveResult(self, newIMG):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если вы сохраняете изображение внутри метода класса, то открывать изображение также желательно внутри метода класса.

res = Image.fromarray(self.arr)
res.save(newIMG)

def StartFilter(self, newIMG):
height, width = len(self.arr), len(self.arr[1])
for y in range(0, height, self.mosaic_size):
for x in range(0, width, self.mosaic_size):
self.__DoGray(y, x)

self.__SaveResult(newIMG)

img = Image.open(input("Введите название изображение: "))
filtr = Filter(img, 10, 50)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Размер блока и количество градаций тоже можно было считывать с консоли. Также должно подаваться количество градаций, исходя из которого уже вычисляется шаг.

filtr.StartFilter(input("Введите новое название изображение: "))
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 res2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.