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
62 changes: 36 additions & 26 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')

correct_path = False
CELL_X_OFFSET = 10
CELL_Y_OFFSET = 10
GREY_GRADATION = 50
while not correct_path:
img_path = input("Enter path to image for filtering: ")
try:
img = Image.open(img_path)
correct_path = True
except:
print("Incorrect path/file format, please try again")
img_matrix = np.array(img)
len_x = len(img_matrix)
len_y = len(img_matrix[1])
cell_x = 0
while cell_x < len_x:
cell_y = 0
while cell_y < len_y:
sum_RGB = 0
sum_RGB += np.sum(img_matrix[cell_x:cell_x + CELL_X_OFFSET, cell_y:cell_y + CELL_Y_OFFSET]) // 3
sum_RGB = int(sum_RGB // 100)
grey_matrix = np.zeros((CELL_X_OFFSET, CELL_Y_OFFSET, 3))
grey_matrix[:] = int(sum_RGB // GREY_GRADATION) * GREY_GRADATION
img_matrix[cell_x:cell_x + CELL_X_OFFSET, cell_y:cell_y + CELL_Y_OFFSET] = grey_matrix
cell_y = cell_y + CELL_Y_OFFSET
cell_x = cell_x + CELL_X_OFFSET
res = Image.fromarray(img_matrix)
correct_path = False
while not correct_path:
res_path = input("Enter path to resulting image: ")
try:
res.save(res_path)
correct_path = True
except:
print("Incorrect path/file format, please try again")
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.