簡體   English   中英

Python GDAL,如何在不解析每個像素的情況下改變亮度

[英]Python GDAL, how to change brightness without parsing each pixel

我目前正在嘗試弄清楚如何在不解析每個像素的情況下增加/減少 a.tiff 文件的亮度(功耗太高)。 現在,使用前端微服務,用戶使用 ng-slider 來更改所需亮度的值,該值直接轉到后面解析它以嘗試計算 new.tiff。

所以,我想知道是否沒有 gdal function 我找不到直接更改圖像並隨意增加/減少亮度!

代碼目前看起來像這樣(也試圖改變對比度,但如果我了解如何改變亮度,我可以找到我的方式):

# 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

希望你知道我找不到的更好的方法。 提前致謝。

第一個線索可能是使用 OpenLayer 為我提供的最后一個功能,但它不再是一個后備解決方案,我正在挖掘它。

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

編輯:對比度和亮度功能僅在 OpenLayer 3 上實現,但在下一個版本(包括我的 OL 5)中沒有實現,所以正確的答案是:這是不可能的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM