From ce1b2ad447468296b69aaa79c5461028deee57e8 Mon Sep 17 00:00:00 2001 From: Alyoshin Maxim Date: Tue, 23 Nov 2021 17:07:13 +0500 Subject: [PATCH 1/5] MAKE-STAGE 1 --- filter.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/filter.py b/filter.py index 4150df2..5f053e5 100644 --- a/filter.py +++ b/filter.py @@ -1,21 +1,21 @@ -from PIL import Image import numpy as np +from PIL import Image img = Image.open("img2.jpg") arr = np.array(img) a = len(arr) a1 = len(arr[1]) -i = 0 -while i < a - 11: +i = 1 +while i < a: j = 0 - while j < a1 - 11: + while j < a1: 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 + m1 = int(arr[n][n1][0]) + m2 = int(arr[n][n1][1]) + m3 = int(arr[n][n1][2]) + M = m1 + m2 + m3 + s += M // 3 s = int(s // 100) for n in range(i, i + 10): for n1 in range(j, j + 10): From 9616922af78855225f574d00873e2007e3ba1b51 Mon Sep 17 00:00:00 2001 From: Alyoshin Maxim Date: Tue, 23 Nov 2021 17:18:21 +0500 Subject: [PATCH 2/5] MAKE-STAGE2 --- filter.py | 71 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/filter.py b/filter.py index 5f053e5..e6a08db 100644 --- a/filter.py +++ b/filter.py @@ -1,28 +1,45 @@ +import os import numpy as np -from PIL import Image -img = Image.open("img2.jpg") -arr = np.array(img) -a = len(arr) -a1 = len(arr[1]) -i = 1 -while i < a: - j = 0 - while j < a1: - s = 0 - for n in range(i, i + 10): - for n1 in range(j, j + 10): - m1 = int(arr[n][n1][0]) - m2 = int(arr[n][n1][1]) - m3 = int(arr[n][n1][2]) - M = m1 + m2 + m3 - s += M // 3 - 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') +from PIL import Image, UnidentifiedImageError + + +def load_img(path: str): + if os.path.isfile(path): + try: + img = Image.open(path) + return np.array(img) + except UnidentifiedImageError: + raise TypeError("Incorrect file type.") + raise FileExistsError("This file doesn't exist.") + + +def convert_to_gray_pixel_art(img: np.ndarray, pixel_size: int = 10, grayscale: int = 50): + width = len(img) + height = len(img[1]) + + for x in range(0, width, pixel_size): + for y in range(0, height, pixel_size): + brightness = 0 + for x1 in range(x, min(x + pixel_size, width)): + for y1 in range(y, min(y + pixel_size, height)): + brightness += sum(int(color) for color in img[x1][y1]) // 3 + brightness = brightness // (pixel_size * pixel_size) + + for x1 in range(x, min(x + pixel_size, width)): + for y1 in range(y, min(y + pixel_size, height)): + img[x1][y1][0] = img[x1][y1][1] = img[x1][y1][2] = brightness - brightness % grayscale + return img + + +def save_img(img: np.ndarray, filename: str): + Image.fromarray(img).save(filename) + + +def main(): + img = load_img("img2.jpg") + gray_image = convert_to_gray_pixel_art(img, 10, 50) + save_img(gray_image, "res.jpg") + + +if __name__ == "__main__": + main() \ No newline at end of file From 88608a27d1c72a54870669bcb4aa85b87eb6d6be Mon Sep 17 00:00:00 2001 From: Alyoshin Maxim Date: Tue, 23 Nov 2021 17:20:39 +0500 Subject: [PATCH 3/5] MAKE-STAGE3 --- filter.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/filter.py b/filter.py index e6a08db..1395033 100644 --- a/filter.py +++ b/filter.py @@ -14,22 +14,13 @@ def load_img(path: str): def convert_to_gray_pixel_art(img: np.ndarray, pixel_size: int = 10, grayscale: int = 50): - width = len(img) - height = len(img[1]) - - for x in range(0, width, pixel_size): - for y in range(0, height, pixel_size): - brightness = 0 - for x1 in range(x, min(x + pixel_size, width)): - for y1 in range(y, min(y + pixel_size, height)): - brightness += sum(int(color) for color in img[x1][y1]) // 3 - brightness = brightness // (pixel_size * pixel_size) - - for x1 in range(x, min(x + pixel_size, width)): - for y1 in range(y, min(y + pixel_size, height)): - img[x1][y1][0] = img[x1][y1][1] = img[x1][y1][2] = brightness - brightness % grayscale - return img + for x in range(0, len(img), pixel_size): + for y in range(0, len(img[0]), pixel_size): + brightness = np.average(img[x: x + pixel_size, y: y + pixel_size]) + img[x: x + pixel_size, y: y + pixel_size] = brightness - brightness % grayscale + + return img def save_img(img: np.ndarray, filename: str): Image.fromarray(img).save(filename) From fc4028e0cca3987fbefa292906a99aebc6d532a8 Mon Sep 17 00:00:00 2001 From: Alyoshin Maxim Date: Tue, 23 Nov 2021 17:27:09 +0500 Subject: [PATCH 4/5] MAKE-STAGE4 --- filter.py | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/filter.py b/filter.py index 1395033..62c0b48 100644 --- a/filter.py +++ b/filter.py @@ -1,7 +1,10 @@ +import argparse import os import numpy as np from PIL import Image, UnidentifiedImageError +STANDARD_PIXEL_SIZE = 10 +STANDARD_GRAYSCALE = 50 def load_img(path: str): if os.path.isfile(path): @@ -9,11 +12,14 @@ def load_img(path: str): img = Image.open(path) return np.array(img) except UnidentifiedImageError: - raise TypeError("Incorrect file type.") - raise FileExistsError("This file doesn't exist.") + print("Incorrect file type.") + exit(1) + print("This file doesn't exist.") + exit(1) -def convert_to_gray_pixel_art(img: np.ndarray, pixel_size: int = 10, grayscale: int = 50): +def conv_to_gray_pixel_art(img: np.ndarray, pixel_size: int = STANDARD_PIXEL_SIZE, + grayscale: int = STANDARD_GRAYSCALE): for x in range(0, len(img), pixel_size): for y in range(0, len(img[0]), pixel_size): brightness = np.average(img[x: x + pixel_size, y: y + pixel_size]) @@ -26,10 +32,28 @@ def save_img(img: np.ndarray, filename: str): Image.fromarray(img).save(filename) + +def convert(input_path: str, output_path: str = None, pixel_size: int = STANDARD_PIXEL_SIZE, + grayscale: int = STANDARD_GRAYSCALE): + img = load_img_as_array(input_path) + gray_image = convert_to_gray_pixel_art(img, pixel_size, grayscale) + file_info = os.path.splitext(input_path) + output_path = output_path or f"{file_info[0]}_pixel{file_info[1]}" + save_img(gray_image, output_path) + + def main(): - img = load_img("img2.jpg") - gray_image = convert_to_gray_pixel_art(img, 10, 50) - save_img(gray_image, "res.jpg") + parser = argparse.ArgumentParser() + parser.add_argument('input_path', type=str, help='Путь до входного изображения.') + parser.add_argument('-op', '--output_path', type=str, default=None, + help=('Путь до выходного изображения. Стандартное значение: ' + '<путь до файла>_pixel.<расширение файла>.')) + parser.add_argument('-ps', '--pixel_size', type=int, default=10, + help=f'Устанавливает размер пикселя. Стандартное значение: {STANDARD_PIXEL_SIZE}.') + parser.add_argument('-gs', '--grayscale', type=int, default=50, + help=f'Устанавливает градацию серого. Стандартное значение: {STANDARD_GRAYSCALE}.') + args = parser.parse_args() + convert(args.input_path, args.output_path, args.pixel_size, args.grayscale) if __name__ == "__main__": From 939b3131cf03ef47fb8894633af4548871a78eb5 Mon Sep 17 00:00:00 2001 From: Alyoshin Maxim Date: Tue, 23 Nov 2021 17:34:15 +0500 Subject: [PATCH 5/5] MAKE-STAGE4_V2 --- filter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/filter.py b/filter.py index 62c0b48..0daff59 100644 --- a/filter.py +++ b/filter.py @@ -35,8 +35,8 @@ def save_img(img: np.ndarray, filename: str): def convert(input_path: str, output_path: str = None, pixel_size: int = STANDARD_PIXEL_SIZE, grayscale: int = STANDARD_GRAYSCALE): - img = load_img_as_array(input_path) - gray_image = convert_to_gray_pixel_art(img, pixel_size, grayscale) + img = load_img(input_path) + gray_image = conv_to_gray_pixel_art(img, pixel_size, grayscale) file_info = os.path.splitext(input_path) output_path = output_path or f"{file_info[0]}_pixel{file_info[1]}" save_img(gray_image, output_path)