aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/bin
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-08-30 01:27:27 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-08-30 01:27:27 +0300
commit0d88d9328d8fea0f5880580dfc9fab57f6a801c5 (patch)
tree1dd84280ffe73da9a880957a64dada13328bc595 /bin
parentfix Pylint warnings (diff)
downloadfilters-0d88d9328d8fea0f5880580dfc9fab57f6a801c5.tar.gz
filters-0d88d9328d8fea0f5880580dfc9fab57f6a801c5.zip
factor common code out
More helpers & more files.
Diffstat (limited to 'bin')
-rw-r--r--bin/__init__.py0
-rw-r--r--bin/box_blur.py38
-rw-r--r--bin/gaussian_blur.py44
-rw-r--r--bin/shift.py52
-rw-r--r--bin/utils/__init__.py0
-rw-r--r--bin/utils/cmd_line.py15
-rw-r--r--bin/utils/image.py16
7 files changed, 165 insertions, 0 deletions
diff --git a/bin/__init__.py b/bin/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/bin/__init__.py
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 <Egor.Tensin@gmail.com>
+# 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 <Egor.Tensin@gmail.com>
+# 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 <Egor.Tensin@gmail.com>
+# 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
--- /dev/null
+++ b/bin/utils/__init__.py
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 <Egor.Tensin@gmail.com>
+# 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 <Egor.Tensin@gmail.com>
+# 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()