简体   繁体   English

Python GDAL,如何在不解析每个像素的情况下改变亮度

[英]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).我目前正在尝试弄清楚如何在不解析每个像素的情况下增加/减少 a.tiff 文件的亮度(功耗太高)。 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.现在,使用前端微服务,用户使用 ng-slider 来更改所需亮度的值,该值直接转到后面解析它以尝试计算 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!所以,我想知道是否没有 gdal function 我找不到直接更改图像并随意增加/减少亮度!

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.第一个线索可能是使用 OpenLayer 为我提供的最后一个功能,但它不再是一个后备解决方案,我正在挖掘它。

https://geoadmin.github.io/ol3/apidoc/ol.layer.Tile.html 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.编辑:对比度和亮度功能仅在 OpenLayer 3 上实现,但在下一个版本(包括我的 OL 5)中没有实现,所以正确的答案是:这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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