How to limit decimals when adjusting a raster array with rasterio

Viewed 25

I have multiple raster images. Currently all the cells have a value which is a count of something. I would like to have this as a percentage. I did this with the following code:

This works fine, but it creates a lot of decimals for some values (33.33333333). Because I'm working with big rasters this greatly increases file size. I do not have need for a lot of decimals, so how do I limit them?

import rasterio as rio

def Percentage(path_in, granule):
    path_out = os.path.join(certain_path, granule + ".jp2")

    for granule in glob.glob(r"path_to_rasters\*"):
    
        with rio.open(path_in) as src:
            # Read as numpy array
            array = src.read()
            profile = src.profile
            array = (array / count) * 100
            
        with rio.open(path_out, 'w', **profile) as dst:
            # Write to disk
            dst.write(array)
0 Answers
Related