Convert priditherpng to argparse
@@ -11,7 +11,7 @@ from bisect import bisect_left
import png
-def dither(out, inp,
+def dither(out, input,
bitdepth=1, linear=False, defaultgamma=1.0, targetgamma=None,
cutoff=0.75):
"""Dither the input PNG `inp` into an image with a smaller bit depth
@@ -56,7 +56,7 @@ def dither(out, inp,
# can only do bit depth reduction, not arbitrary palette changes.
import operator
maxval = 2**bitdepth - 1
- r = png.Reader(file=inp)
+ r = png.Reader(file=input)
# If image gamma is 1 or gamma is not present and we are assuming a
# value of 1, then it is faster to pass a maxval parameter to
# asFloat (the multiplications get combined). With gamma, we have
@@ -146,33 +146,31 @@ def dither(out, inp,
def main(argv=None):
- # http://www.python.org/doc/2.4.4/lib/module-getopt.html
- from getopt import getopt
import sys
+
+ # https://docs.python.org/3.5/library/argparse.html
+ import argparse
+
+ parser = argparse.ArgumentParser()
+
if argv is None:
argv = sys.argv
- opt,argv = getopt(argv[1:], 'b:c:g:lo:')
- k = {}
- for o,v in opt:
- if o == '-b':
- k['bitdepth'] = int(v)
- if o == '-c':
- k['cutoff'] = float(v)
- if o == '-g':
- k['defaultgamma'] = float(v)
- if o == '-l':
- k['linear'] = True
- if o == '-o':
- k['targetgamma'] = float(v)
- if o == '-?':
- print("pipdither [-b bits] [-c cutoff] [-g assumed-gamma] [-l] [in.png]", file=sys.stderr)
-
- if len(argv) > 0:
- f = open(argv[0], 'rb')
- else:
- f = png.binary_stdin()
- return dither(png.binary_stdout(), f, **k)
+ progname, *args = argv
+
+ parser.add_argument("--bitdepth", type=int, default=1, help="bitdepth of output")
+ parser.add_argument("--cutoff", type=float, default=0.75, help="cutoff to select adjacent output values")
+ parser.add_argument("--defaultgamma", type=float,
+ default=1.0, help="gamma value to use when no gamma in input")
+ parser.add_argument("--linear", type=bool,
+ help="force linear input")
+ parser.add_argument("--targetgamma", type=float,
+ help="gamma to use in output (target), defaults to input gamma")
+ parser.add_argument("input", nargs="?", default="-", type=png.cli_open)
+
+ ns = parser.parse_args(args)
+
+ return dither(png.binary_stdout(), **vars(ns))
if __name__ == '__main__':
