简体   繁体   中英

Python GDAL, how to change brightness without parsing each pixel

I'm currently trying to figure out how to increase/decrease the brightness of a.tiff file without parsing each pixel (too high power consumption). Right now, using the front micro-service, the user uses a ng-slider to change the value of the desired brightness,which goes directly to the back where it is parsed to try to compute a new.tiff.

So, I'm wondering if there isn't a gdal function I can't find to directly alter the images and increase/decrease the brightness at will!

The code currently looks like this (also trying to change the contrast, but I could find my way if I understand how to change the brightness):

# Contrast & Luminosity
def get_correctMap(path, luminosity, contrast):
        ds = gdal.Open(image_path)

        #To normalize
        band1 = ds.GetRasterBand(1)
        #Get the max value
        maxValue = int(2**16 -1)
        if band1.DataType == gdal.GDT_UInt16:
            maxValue = int(2**16 -1)
        elif band1.DataType == gdal.GDT_Byte:
            maxValue = int(2**8 -1)
        else:
            LOGGER.info(f"band type {band1.DataType} not handled: use default size of value (16 bits)")

        band1 = ds.ReadAsArray(0,0,ds.RasterXSize,ds.RasterYSize)[0]
        band2 = ds.ReadAsArray(0,0,ds.RasterXSize,ds.RasterYSize)[1]
        band3 = ds.ReadAsArray(0,0,ds.RasterXSize,ds.RasterYSize)[2] 

        for x in range(0,ds.RasterXSize):
                for y in range(0,ds.RasterXSize):

                    r = float(band1[x,y]) / maxValue
                    g = float(band2[x,y]) / maxValue
                    b = float(band3[x,y]) / maxValue

                    #Convert to HLS them apply luminosity and contrast
                    (h,l,s) = colorsys.rgb_to_hls(r, g, b)

                    l = min(max(0, l + (l - 0.5)*(luminosity - 0.5)) , 1)
                    s = min(max(0, s + (s - 0.5)*(contrast - 0.5)) , 1)

                    (r,g,b) = colorsys.hls_to_rgb(h, l, s)

                    band1[x,y] = int(r * maxValue)
                    band2[x,y] = int(g * maxValue)
                    band3[x,y] = int(b * maxValue)


        #Need to save the changes, but obviously already too long to pursue this way 
        #and save the news bands
        ds.flushCache()

        return path

Hope you know a better way I can't find. Thanks in advance.

A first lead could be to use the last features provide by OpenLayer for me, but it is not a back solution anymore, I'm digging it.

https://geoadmin.github.io/ol3/apidoc/ol.layer.Tile.html

EDIT: The constrast and luminosity feature are only implemented on OpenLayer 3 but not in the next version (including mine OL 5), so the proper answer is: it is not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM