From 28fbdd6d697b6449e2fd83196a926957c9beaf23 Mon Sep 17 00:00:00 2001 From: LocenokLOL <72749980+LocenokLOL@users.noreply.github.com> Date: Fri, 10 Dec 2021 23:05:40 +0500 Subject: [PATCH] Update filter.py --- filter.py | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/filter.py b/filter.py index 4150df2..e824fda 100644 --- a/filter.py +++ b/filter.py @@ -2,27 +2,19 @@ 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 convert_to_mosaic(img_in = "img2.jpg", img_out="res.jpg", block_size = 10, gradation_step = 50): + img_in = Image.open(img_in) + arr = np.array(img_in) + i = 0 + while i < len(arr[0]): + j = 0 + while j < len(arr[0]): + s = np.mean(arr[i:i = block_size,j:j = block_size][:]) + arr[i:i = block_size,j:j = block_size][:]] = int(s//gradation_step) * gradation_step + j = j + block_size + i = i + block_size + res = Image.formarray(arr) + res.save(img_out) + + +convert_to_mosaic()