#!/usr/bin/env python3 ############################################################################ # # MODULE: i.colors.enhance (former i.landsat.rgb) # # AUTHOR(S): Markus Neteler, original author # Hamish Bowman, scripting enhancements # Converted to Python by Glynn Clements # # PURPOSE: create pretty RGBs: the trick is to remove outliers # using percentiles (area under the histogram curve) # # SPDX-FileCopyrightText: 2006, 2008, 2012-2014 GRASS Development Team # SPDX-License-Identifier: GPL-2.0-or-later # # TODO: implement better brightness control ############################################################################# # %module # % description: Performs auto-balancing of colors for RGB images. # % keyword: imagery # % keyword: RGB # % keyword: satellite # % keyword: colors # %end # %option G_OPT_R_INPUT # % key: red # % description: Name of red channel # %end # %option G_OPT_R_INPUT # % key: green # % description: Name of green channel # %end # %option G_OPT_R_INPUT # % key: blue # % description: Name of blue channel # %end # %option # % key: strength # % type: double # % description: Cropping intensity (upper brightness level) # % options: 0-100 # % answer : 98 # % required: no # %end # %flag # % key: f # % description: Extend colors to full range of data on each channel # % guisection: Colors # %end # %flag # % key: p # % description: Preserve relative colors, adjust brightness only # % guisection: Colors # %end # %flag # % key: r # % description: Reset to standard color range # % guisection: Colors # %end # %flag # % key: s # % description: Process bands serially (default: run in parallel) # %end import sys import multiprocessing as mp import grass.script as gs def get_percentile(map, percentiles): # TODO: generalize for any list length val1 = percentiles[0] val2 = percentiles[1] values = "%s,%s" % (val1, val2) s = gs.read_command("r.quantile", input=map, percentiles=values, quiet=True) val_str1 = s.splitlines()[0].split(":")[2] val_str2 = s.splitlines()[1].split(":")[2] return (float(val_str1), float(val_str2)) # wrapper to handle multiprocesses communications back to the parent def get_percentile_mp(map, percentiles, conn): # Process() doesn't like storing connection parts in # separate dictionaries, only wants to pass through tuples, # so instead of just sending the sending the pipe we have to # send both parts then keep the one we want. ?? output_pipe, input_pipe = conn input_pipe.close() result = get_percentile(map, percentiles) gs.debug("child (%s) (%.1f, %.1f)" % (map, result[0], result[1])) output_pipe.send(result) output_pipe.close() def set_colors(map, v0, v1): rules = "".join( ["0% black\n", "%f black\n" % v0, "%f white\n" % v1, "100% white\n"] ) gs.write_command("r.colors", map=map, rules="-", stdin=rules, quiet=True) def main(): red = options["red"] green = options["green"] blue = options["blue"] brightness = options["strength"] full = flags["f"] preserve = flags["p"] reset = flags["r"] do_mp = True if flags["s"]: do_mp = False check = True for m in [red, green, blue]: ex = gs.find_file(m) if ex["name"] == "": check = False gs.warning("Raster map <{}> not found ".format(m)) if not check: gs.fatal("At least one of the input raster map was not found") # 90 or 98? MAX value controls brightness # think of percent (0-100), must be positive or 0 # must be more than "2" ? if full: for i in [red, green, blue]: gs.run_command("r.colors", map=i, color="grey", quiet=True) sys.exit(0) if reset: for i in [red, green, blue]: gs.run_command("r.colors", map=i, color="grey255", quiet=True) sys.exit(0) if not preserve: if do_mp: gs.message(_("Processing...")) # set up jobs and launch them proc = {} conn = {} for i in [red, green, blue]: conn[i] = mp.Pipe() proc[i] = mp.Process( target=get_percentile_mp, args=( i, ["2", brightness], conn[i], ), ) proc[i].start() gs.percent(1, 2, 1) # collect results and wait for jobs to finish for i in [red, green, blue]: output_pipe, input_pipe = conn[i] (v0, v1) = input_pipe.recv() gs.debug("parent (%s) (%.1f, %.1f)" % (i, v0, v1)) input_pipe.close() proc[i].join() set_colors(i, v0, v1) gs.percent(1, 1, 1) else: for i in [red, green, blue]: gs.message(_("Processing...")) (v0, v1) = get_percentile(i, ["2", brightness]) gs.debug("<%s>: min=%f max=%f" % (i, v0, v1)) set_colors(i, v0, v1) else: all_max = 0 all_min = 999999 if do_mp: gs.message(_("Processing...")) # set up jobs and launch jobs proc = {} conn = {} for i in [red, green, blue]: conn[i] = mp.Pipe() proc[i] = mp.Process( target=get_percentile_mp, args=( i, ["2", brightness], conn[i], ), ) proc[i].start() gs.percent(1, 2, 1) # collect results and wait for jobs to finish for i in [red, green, blue]: output_pipe, input_pipe = conn[i] (v0, v1) = input_pipe.recv() gs.debug("parent (%s) (%.1f, %.1f)" % (i, v0, v1)) input_pipe.close() proc[i].join() all_min = min(all_min, v0) all_max = max(all_max, v1) gs.percent(1, 1, 1) else: for i in [red, green, blue]: gs.message(_("Processing...")) (v0, v1) = get_percentile(i, ["2", brightness]) gs.debug("<%s>: min=%f max=%f" % (i, v0, v1)) all_min = min(all_min, v0) all_max = max(all_max, v1) gs.debug("all_min=%f all_max=%f" % (all_min, all_max)) for i in [red, green, blue]: set_colors(i, all_min, all_max) # write cmd history: mapset = gs.gisenv()["MAPSET"] for i in [red, green, blue]: if gs.find_file(i)["mapset"] == mapset: gs.raster_history(i) if __name__ == "__main__": options, flags = gs.parser() main()