From 0d88d9328d8fea0f5880580dfc9fab57f6a801c5 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 30 Aug 2016 01:27:27 +0300 Subject: factor common code out More helpers & more files. --- bin/__init__.py | 0 bin/box_blur.py | 38 +++++++++++++++++++++++++++++++++++++ bin/gaussian_blur.py | 44 +++++++++++++++++++++++++++++++++++++++++++ bin/shift.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ bin/utils/__init__.py | 0 bin/utils/cmd_line.py | 15 +++++++++++++++ bin/utils/image.py | 16 ++++++++++++++++ 7 files changed, 165 insertions(+) create mode 100644 bin/__init__.py create mode 100644 bin/box_blur.py create mode 100644 bin/gaussian_blur.py create mode 100644 bin/shift.py create mode 100644 bin/utils/__init__.py create mode 100644 bin/utils/cmd_line.py create mode 100644 bin/utils/image.py (limited to 'bin') diff --git a/bin/__init__.py b/bin/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bin/box_blur.py b/bin/box_blur.py new file mode 100644 index 0000000..9d5d1cd --- /dev/null +++ b/bin/box_blur.py @@ -0,0 +1,38 @@ +# Copyright 2016 (c) Egor Tensin +# This file is part of the "Simple image filters" project. +# For details, see https://github.com/egor-tensin/filters. +# Distributed under the MIT License. + +import argparse +import sys + +from .utils import cmd_line, image +from filters.convolution import convolve +from filters.kernel.box_blur import gen_kernel + +DEFAULT_RADIUS = 1 + +def _main_box_blur(img_path, radius=DEFAULT_RADIUS, output_path=None): + img = image.load_grayscale(img_path) + kernel = gen_kernel(radius) + output = convolve(img, kernel) + if output_path is None: + image.show(output) + else: + image.save(output_path, output) + +def _parse_args(args=sys.argv): + parser = argparse.ArgumentParser() + parser.add_argument('img_path') + parser.add_argument('--output', '-o', + dest='output_path', default=None) + parser.add_argument('--radius', '-r', + type=cmd_line.parse_non_negative_integer, + default=DEFAULT_RADIUS) + return parser.parse_args(args[1:]) + +def _main(args=sys.argv): + _main_box_blur(**vars(_parse_args(args))) + +if __name__ == '__main__': + _main() diff --git a/bin/gaussian_blur.py b/bin/gaussian_blur.py new file mode 100644 index 0000000..d530547 --- /dev/null +++ b/bin/gaussian_blur.py @@ -0,0 +1,44 @@ +# Copyright 2016 (c) Egor Tensin +# This file is part of the "Simple image filters" project. +# For details, see https://github.com/egor-tensin/filters. +# Distributed under the MIT License. + +import argparse +import sys + +from .utils import cmd_line, image +from filters.convolution import convolve +from filters.kernel.gaussian_blur import gen_kernel + +DEFAULT_SIGMA = 1. +DEFAULT_RADIUS = 1 + +def _main_gaussian_blur( + img_path, radius=DEFAULT_RADIUS, sigma=DEFAULT_SIGMA, + output_path=None): + + img = image.load_grayscale(img_path) + kernel = gen_kernel(radius, sigma) + output = convolve(img, kernel) + if output_path is None: + image.show(output) + else: + image.save(output_path, output) + +def _parse_args(args=sys.argv): + parser = argparse.ArgumentParser() + parser.add_argument('img_path') + parser.add_argument('--output', '-o', + dest='output_path', default=None) + parser.add_argument('--sigma', '-s', + type=float, default=DEFAULT_SIGMA) + parser.add_argument('--radius', '-r', + type=cmd_line.parse_non_negative_integer, + default=DEFAULT_RADIUS) + return parser.parse_args(args[1:]) + +def _main(args=sys.argv): + _main_gaussian_blur(**vars(_parse_args(args))) + +if __name__ == '__main__': + _main() diff --git a/bin/shift.py b/bin/shift.py new file mode 100644 index 0000000..8dfd76a --- /dev/null +++ b/bin/shift.py @@ -0,0 +1,52 @@ +# Copyright 2016 (c) Egor Tensin +# This file is part of the "Simple image filters" project. +# For details, see https://github.com/egor-tensin/filters. +# Distributed under the MIT License. + +import argparse +import sys + +from .utils import cmd_line, image +from filters.convolution import convolve +from filters.kernel.shift import Direction + +DEFAULT_DIRECTION = Direction.SOUTH_EAST +DEFAULT_DISTANCE = 1 + +def _main_shift( + img_path, direction=DEFAULT_DIRECTION, distance=DEFAULT_DISTANCE, + output_path=None): + + img = image.load_grayscale(img_path) + kernel = direction.gen_kernel(distance) + output = convolve(img, kernel) + if output_path is None: + image.show(output) + else: + image.save(output_path, output) + +def _parse_direction(s): + try: + return Direction(s) + except ValueError: + raise argparse.ArgumentTypeError('invalid direction: ' + s) + +def _parse_args(args=sys.argv): + parser = argparse.ArgumentParser() + parser.add_argument('img_path') + parser.add_argument('--output', '-o', + dest='output_path', default=None) + parser.add_argument('--direction', '-d', + type=_parse_direction, + choices=Direction, + default=DEFAULT_DIRECTION) + parser.add_argument('--distance', '-n', + type=cmd_line.parse_non_negative_integer, + default=DEFAULT_DISTANCE) + return parser.parse_args(args[1:]) + +def _main(args=sys.argv): + _main_shift(**vars(_parse_args(args))) + +if __name__ == '__main__': + _main() diff --git a/bin/utils/__init__.py b/bin/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bin/utils/cmd_line.py b/bin/utils/cmd_line.py new file mode 100644 index 0000000..6bfeafe --- /dev/null +++ b/bin/utils/cmd_line.py @@ -0,0 +1,15 @@ +# Copyright 2016 (c) Egor Tensin +# This file is part of the "Simple image filters" project. +# For details, see https://github.com/egor-tensin/filters. +# Distributed under the MIT License. + +import argparse + +def parse_non_negative_integer(s): + try: + x = int(s) + except ValueError: + raise argparse.ArgumentTypeError('must be a non-negative integer: ' + s) + if x < 0: + raise argparse.ArgumentTypeError('must be a non-negative integer: ' + s) + return x diff --git a/bin/utils/image.py b/bin/utils/image.py new file mode 100644 index 0000000..1dd64cd --- /dev/null +++ b/bin/utils/image.py @@ -0,0 +1,16 @@ +# Copyright 2016 (c) Egor Tensin +# This file is part of the "Simple image filters" project. +# For details, see https://github.com/egor-tensin/filters. +# Distributed under the MIT License. + +import cv2 + +def load_grayscale(path): + return cv2.imread(path, cv2.IMREAD_GRAYSCALE) + +def save(path, img): + cv2.imwrite(path, img) + +def show(img, window_title=None): + cv2.imshow(window_title, img) + cv2.waitKey() -- cgit v1.2.3