~cryptoxic/dumb-dithering

ad95985fb92c907abc617b7a00ec7acc69609282 — cryptoxic 2 months ago e923d82 master
Add commandline parsing
1 files changed, 41 insertions(+), 3 deletions(-)

M dumb_dithering.py
M dumb_dithering.py => dumb_dithering.py +41 -3
@@ 1,5 1,7 @@
#!/usr/bin/python
import os
import json
import argparse
from math import sqrt
from collections import namedtuple
from PySide6.QtGui import QImage


@@ 7,9 9,22 @@ from PySide6.QtGui import QImage
Rgb = namedtuple('Rgb', 'r, g, b')

def main():
    args = build_parser()
    args.theme = ''.join(args.theme)
    args.source = ''.join(args.source)
    args.output = ''.join(args.output)

    if not os.path.exists(args.source):
        print(f"Could not find source file '{str(args.source)}'")
        return

    themes = json.load(open("themes.json"))
    theme = themes.get(args.theme)
    if not theme:
        print(f"Could not find theme '{args.theme}' defined themes:")
        print(f"  [{', '.join(themes.keys())}]")
        return

    theme = themes["solarized"]
    rgb_theme = {}
    for name, color in theme.items():
        triple = color.strip("#")


@@ 19,7 34,7 @@ def main():
        rgb_theme[name] = Rgb(r=red, g=green, b=blue)

    pic = QImage()
    pic.load("test2.jpg")
    pic.load(args.source)

    lines = pic.sizeInBytes() // pic.bytesPerLine()
    for line in range(0, lines):


@@ 35,7 50,7 @@ def main():
            pic_line[x] = rgb_theme[sorted_dist[0][0]].r
            pic_line[x+1] = rgb_theme[sorted_dist[0][0]].g
            pic_line[x+2] = rgb_theme[sorted_dist[0][0]].b
    pic.save("newtest2.jpg")
    pic.save(args.output)


def distance(initial, target):


@@ 45,5 60,28 @@ def distance(initial, target):
    return sqrt(dist_r + dist_g + dist_b)


def build_parser():
    """Construct an argument parser for command line calls."""
    parser = argparse.ArgumentParser(description="Dither an image to a custom theme template")
    parser.add_argument(
        "-t",
        "--theme",
        nargs=1,
        type=str,
        help="the theme to use",
        default='solarized'
    )
    parser.add_argument(
        "source",
        metavar="SOURCE",
        nargs=1,
        # type=argparse.FileType("r"),
        type=str,
        help="the source image filename",
    )
    parser.add_argument("output", metavar="OUTPUT", nargs=1, type=str, help="the output filename")
    return parser.parse_args()


if __name__ == "__main__":
    main()