From c5b8feb17e7e0bd6c376e5e8f0fbcdd69e12aa0d Mon Sep 17 00:00:00 2001 From: kris Date: Fri, 15 Jan 2021 22:25:06 +0000 Subject: [PATCH] Parametrize dither patterns as command-line argument --- convert.py | 7 ++++--- dither_pattern.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/convert.py b/convert.py index 3005bd5..5c0078d 100644 --- a/convert.py +++ b/convert.py @@ -25,6 +25,9 @@ def main(): "--lookahead", type=int, default=4, help=("How many pixels to look ahead to compensate for NTSC colour " "artifacts.")) + parser.add_argument('--dither', type=str, + choices=list(dither_pattern.PATTERNS.keys()), + default=dither_pattern.DEFAULT_PATTERN) args = parser.parse_args() palette = palette_py.Palette() @@ -34,9 +37,7 @@ def main(): image = image_py.open(screen.X_RES, screen.Y_RES, args.input) # image_rgb.show() - # dither = dither_pattern.FloydSteinbergDither() - # dither = dither_pattern.BuckelsDither() - dither = dither_pattern.JarvisDither() + dither = dither_pattern.PATTERNS[args.dither]() start = time.time() output_4bit, output_rgb = dither_pyx.dither_image( diff --git a/dither_pattern.py b/dither_pattern.py index ed03ef3..ca2b742 100644 --- a/dither_pattern.py +++ b/dither_pattern.py @@ -7,6 +7,7 @@ class DitherPattern: class FloydSteinbergDither(DitherPattern): + """Floyd-Steinberg dither.""" # 0 * 7 # 3 5 1 PATTERN = np.array(((0, 0, 7), (3, 5, 1)), @@ -16,6 +17,7 @@ class FloydSteinbergDither(DitherPattern): class BuckelsDither(DitherPattern): + """Default dither from bmp2dhr.""" # 0 * 2 1 # 1 2 1 0 # 0 1 0 0 @@ -25,6 +27,8 @@ class BuckelsDither(DitherPattern): class JarvisDither(DitherPattern): + """Jarvis dithering.""" + # 0 0 X 7 5 # 3 5 7 5 3 # 1 3 5 3 1 @@ -32,3 +36,12 @@ class JarvisDither(DitherPattern): dtype=np.float32).reshape(3, 5, 1) / np.float32(48) ORIGIN = (0, 2) + +PATTERNS = { + 'floyd': FloydSteinbergDither, + 'floyd-steinberg': FloydSteinbergDither, + 'buckels': BuckelsDither, + 'jarvis': JarvisDither +} + +DEFAULT_PATTERN = 'jarvis' \ No newline at end of file