Used argparse instead of optparse in TeamColorizer
This commit is contained in:
parent
1f3674dcf6
commit
f07f893d34
1 changed files with 84 additions and 78 deletions
|
@ -1,40 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Usage: TeamColorizer [--color=COLOR] input-filename output-filename
|
||||
|
||||
Map the magenta team-color patches in the input image to red (or a custom
|
||||
color) in the output image, copy the result to output.
|
||||
|
||||
COLOR should be a normal Wesnoth team color. Note that the default (when no
|
||||
options are given) is to use red (255, 0, 0). Advanced options include:
|
||||
|
||||
-h, -?, --help
|
||||
These options display this help and then exit.
|
||||
-d, --dryrun
|
||||
Print the command to be executed, but don't actually generate the output
|
||||
image.
|
||||
-v, --verbose
|
||||
Print extra information about what is going on.
|
||||
-x, --hex
|
||||
Use base 16 for defining custom colors. Works with the -r, -g, and -b
|
||||
options.
|
||||
-l, --luminance
|
||||
Use luminance instead of average value for computing color brightness when
|
||||
mapping colors. This produces results that are noticeably poorer than those
|
||||
produced by the in-game algorithm (which is used in the absence of -l).
|
||||
-rRED, --red=RED
|
||||
Set the desired red value to RED. Should be an integer between 0 and 255,
|
||||
or a hex value in the same range if -x is given.
|
||||
-gGREEN, --green=GREEN, -bBLUE, --blue=BLUE
|
||||
These work the same as -r, but for blue and green values.
|
||||
--color=COLOR
|
||||
Causes -r, -g, and -b to be ignored. Sets the desired color. Use Wesnoth
|
||||
team colors, like 'red' or 'blue'. This method uses a more complex color
|
||||
definition but produces results identical to the in-game algorithm. Extra
|
||||
colors available: 'magenta' (which does nothing), 'cyan', 'yellow' and
|
||||
'pink'.
|
||||
"""
|
||||
import sys, getopt, subprocess
|
||||
import sys, argparse, subprocess
|
||||
|
||||
# Note: Luminance formula taken from the Wikipedia article on luminance:
|
||||
# http://en.wikipedia.org/wiki/Luminance_(colorimetry)
|
||||
|
@ -144,19 +110,18 @@ def convert_color(color, hex=False):
|
|||
internal table, and the resulting values are used. A failed table lookup
|
||||
results in an error message.
|
||||
'''
|
||||
if type(color) == str:
|
||||
if isinstance(color, str):
|
||||
if color in team_colors:
|
||||
return team_colors[color]
|
||||
else:
|
||||
print("Couldn't find color '%s'." % color, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
new = {}
|
||||
base = 10
|
||||
if hex: base = 16
|
||||
base = 16 if hex else 10
|
||||
for c in 'rgb':
|
||||
if type(color['mid'][c]) == str:
|
||||
if isinstance(color['mid'][c], str):
|
||||
try:
|
||||
new[c] = int(color[c], base)
|
||||
new[c] = int(color['mid'][c], base)
|
||||
except ValueError:
|
||||
print("Couldn't convert color value %s='%s' using "\
|
||||
"base %d. Did you forget -x?" %\
|
||||
|
@ -221,45 +186,86 @@ def get_convert_options(color):
|
|||
return options
|
||||
|
||||
if __name__ == '__main__':
|
||||
(options, arguments) = getopt.getopt(sys.argv[1:], "h?dvxlr:g:b:",
|
||||
['help', 'dryrun', 'verbose', 'hex', 'luminance', 'red=', 'green=', 'blue=', 'color='])
|
||||
verbose = 0
|
||||
dryrun = False
|
||||
hex = False
|
||||
method = "average"
|
||||
exclude = []
|
||||
color = default_color
|
||||
for (opt, val) in options:
|
||||
if opt in ('-?', '-h', '--help'):
|
||||
print(__doc__)
|
||||
sys.exit(0)
|
||||
elif opt in ('-d', '--dryrun'):
|
||||
dryrun = True
|
||||
verbose += 1
|
||||
elif opt in ('-v', '--verbose'):
|
||||
verbose += 1
|
||||
elif opt in ('-x', '--hex'):
|
||||
hex = True
|
||||
elif opt in ('-l', '--luminance'):
|
||||
method = "luminance"
|
||||
elif opt in ('-r', '--red'):
|
||||
if type(color) == dict:
|
||||
color['mid']['r'] = val
|
||||
elif opt in ('-g', '--green'):
|
||||
if type(color) == dict:
|
||||
color['mid']['g'] = val
|
||||
elif opt in ('-b', '--blue'):
|
||||
if type(color) == dict:
|
||||
color['mid']['b'] = val
|
||||
elif opt == '--color':
|
||||
color = val
|
||||
parser = argparse.ArgumentParser(
|
||||
description="""Map the magenta team-color patches in the input image to red (or a custom
|
||||
color) in the output image, copy the result to output.""",
|
||||
formatter_class=argparse.RawTextHelpFormatter)
|
||||
parser.add_argument(
|
||||
"-d", "--dryrun",
|
||||
action="store_true",
|
||||
help="""Print the command to be executed, but don't actually
|
||||
generate the output image.""")
|
||||
parser.add_argument(
|
||||
"-v", "--verbose",
|
||||
action="count",
|
||||
default=0,
|
||||
help="""Print extra information about what is going on.""")
|
||||
parser.add_argument(
|
||||
"-x", "--hex",
|
||||
action="store_true",
|
||||
help="""Use base 16 for defining custom colors. Works with the
|
||||
-r, -g, and -b options.""")
|
||||
parser.add_argument(
|
||||
"-l", "--luminance",
|
||||
action="store_const",
|
||||
dest="method",
|
||||
const="luminance",
|
||||
default="average",
|
||||
help="""Use luminance instead of average value for computing
|
||||
color brightness when mapping colors. This produces
|
||||
results that are noticeably poorer than those produced
|
||||
by the in-game algorithm (which is used in the absence
|
||||
of -l).""")
|
||||
parser.add_argument(
|
||||
"-r", "--red",
|
||||
action="store",
|
||||
default=None,
|
||||
help="""Set the desired red value to RED. Should be an integer
|
||||
between 0 and 255, or a hex value in the same range if
|
||||
-x is given.""")
|
||||
parser.add_argument(
|
||||
"-g", "--green",
|
||||
action="store",
|
||||
default=None,
|
||||
help="Same as -r, but for green value.")
|
||||
parser.add_argument(
|
||||
"-b", "--blue",
|
||||
action="store",
|
||||
default=None,
|
||||
help="Same as -r, but for blue value.")
|
||||
parser.add_argument(
|
||||
"--color",
|
||||
action="store",
|
||||
choices=sorted(team_colors.keys()),
|
||||
default=None,
|
||||
help="""Causes -r, -g, and -b to be ignored. Sets the desired
|
||||
color (default: red). This method uses a more complex
|
||||
color definition but produces results identical to the
|
||||
in-game algorithm.""")
|
||||
parser.add_argument(
|
||||
"input_file",
|
||||
action="store")
|
||||
parser.add_argument(
|
||||
"output_file",
|
||||
action="store")
|
||||
namespace = parser.parse_args()
|
||||
verbose = namespace.verbose
|
||||
dryrun = namespace.dryrun
|
||||
if dryrun:
|
||||
verbose = max(1, verbose)
|
||||
hex = namespace.hex
|
||||
method = namespace.method
|
||||
color = namespace.color
|
||||
if not color:
|
||||
r = 255 if namespace.red is None else namespace.red
|
||||
g = 0 if namespace.green is None else namespace.green
|
||||
b = 0 if namespace.blue is None else namespace.blue
|
||||
color = { 'mid': { 'r': r, 'g': g, 'b': b },
|
||||
'max': { 'r':0xff, 'g':0xff, 'b':0xff },
|
||||
'min': { 'r':0x00, 'g':0x00, 'b':0x00 }}
|
||||
|
||||
if len(arguments) != 2:
|
||||
print("Invalid number of arguments: %d (required: 2)" % len(arguments))
|
||||
print(__doc__)
|
||||
sys.exit(1)
|
||||
else:
|
||||
(infilename, outfilename) = arguments
|
||||
infilename = namespace.input_file
|
||||
outfilename = namespace.output_file
|
||||
|
||||
color = convert_color(color, hex)
|
||||
options = get_convert_options(color)
|
||||
|
|
Loading…
Add table
Reference in a new issue