def scale_image(rootpath, inputimage, exact= True, debug=False): """ :param rootpath: :param inputimage: :param scaledout: :param exact: :param debug: :return: the output image path """ if exact: # scale all the bands exactly from 0 to 255 with orfeo print("scaling image exactly from 0 to 255, wait....") scaledout = rootpath + "rasterRescaled0_255.tif" out, err = utility.convert_raster(inputimage, scaledout) if debug: print(out) if err: if not (err == "\r\n" or err == "\r" or err == "\n"): # windows, oldmac, unix print(err) sys.exit(1) else: # scale all the bands within 0 to 255 with gdal print("scaling image within 0 to 255, wait....") scaledout = rootpath + "rasterRescaledWithin0_255.tif" out, err = utility.run_tool(['gdal_translate.exe', inputimage, scaledout, "-scale"]) if debug: print(out) if err: if not (err == '\r\n' or err == '\r' or err == '\n'): # windows, oldmac, unix print(err) print("the input image could not be rescaled, script is stopping") sys.exit(1) return scaledout
def scale_ndvi(path,inimgfrmt = ['.tif'], debug = False): """ Scale NDVI images from 0 to 255 note: this scaling seems not to work, that's why we use the min/max values in heralick_NDI() :param path: tha directory that contains the NDI images :param inimgfrmt: list of formats to filter :param debug: complete messages? :return: None """ imgs = os.listdir(path) # make a "scaled" directory if it does not exist if not os.path.exists(path + "/scaled"): os.mkdir(path + "/scaled") # iterate the items in the folder for i in imgs: wrong = False # discard the directories if os.path.isfile(path + '/' + i) and (os.path.splitext(path + '/' + i)[-1] in inimgfrmt): print("scaling " + i) # scale within 0 and 255 # out,err = utility.run_tool( ['gdal_translate.exe', path+"/"+i, path+"/scaled/"+i+"_scaled.tif", '-scale', '-1','1', '0', '255']) # ####path+'/'+i,path+'/scaled/'+i+'_scaled.tif','uint8', 'linear' ) # scale exactly from 0 to 255 with orfeo out, err = utility.convert_raster(path + '/' + i, path + "/scaled/" + i + "_scaled.tif") if debug: print(out) if err: if not (err == "\r\n" or err == "\r" or err == "\n"): # windows, oldmac, unix print(err) wrong = True # if some image could not rescaled exit if wrong: print("some NDI images could not be rescaled, script is stopping") sys.exit(1)