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
61 changes: 35 additions & 26 deletions filter.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
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')
import sys


def get_mosaic_image(image, mosaic_size=10, gradation=4):
threshold = 255 // gradation
image_tensor = np.array(image).astype(int)
image_length = len(image_tensor)
image_height = len(image_tensor[0])
i = 0
while i < image_length:
j = 0
while j < image_height:
sector = image_tensor[i: i + mosaic_size, j: j + mosaic_size]
colors_sum = np.sum(sector)
average_color = int(colors_sum // (mosaic_size ** 2))
set_color(int(average_color // threshold) * threshold / 3, image_tensor, mosaic_size, i, j)
j += mosaic_size
i += mosaic_size
return Image.fromarray(np.uint8(image_tensor))


def set_color(new_color, tensor, mosaic_size, i, j):
for sector_i in range(i, i + mosaic_size):
for sector_j in range(j, j + mosaic_size):
for c in range(3):
tensor[sector_i][sector_j][c] = new_color


if __name__ == "__main__":
source_name = sys.argv[1]
output_name = sys.argv[2]

source_img = Image.open(source_name)
image_tensor = np.array(source_img).astype(int)
get_mosaic_image(image_tensor, mosaic_size=10, gradation=5).save(output_name)
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.