def _histogram_stretch(self, data, **kwargs):
limits = {}<EOL>for x in range(<NUM_LIT:3>):<EOL><INDENT>band = data[:,:,x]<EOL>try:<EOL><INDENT>limits[x] = np.percentile(band, kwargs.get("<STR_LIT>", [<NUM_LIT:0>,<NUM_LIT:100>]))<EOL><DEDENT>except IndexError:<EOL><INDENT>return data<EOL><DEDENT><DEDENT>for x in range(<NUM_LIT:3>):<EOL><INDENT>band = data[:,:,x]<EOL>if <NUM_LIT:0> in band:<EOL><INDENT>band = np.ma.masked_values(band, <NUM_LIT:0>).compressed()<EOL><DEDENT>top = limits[x][<NUM_LIT:1>]<EOL>bottom = limits[x][<NUM_LIT:0>]<EOL>if top != bottom: <EOL><INDENT>data[:,:,x] = (data[:,:,x] - bottom) / float(top - bottom) * <NUM_LIT><EOL><DEDENT><DEDENT>data = np.clip(data, <NUM_LIT:0>, <NUM_LIT:255>).astype("<STR_LIT>")<EOL>if "<STR_LIT>" in kwargs:<EOL><INDENT>invGamma = <NUM_LIT:1.0> / kwargs['<STR_LIT>']<EOL>lut = np.array([((i / <NUM_LIT>) ** invGamma) * <NUM_LIT:255><EOL>for i in np.arange(<NUM_LIT:0>, <NUM_LIT>)]).astype("<STR_LIT>")<EOL>data = np.take(lut, data)<EOL><DEDENT>return data<EOL>
perform a contrast stretch and/or gamma adjustment