我目前正在尝试找出如何在不解析每个像素的情况下增加/减少.tiff文件的亮度(功耗太高)。现在,使用前端微服务,用户可以使用ng-slider更改所需亮度的值,该亮度将直接转到解析后的后端以尝试计算新的.tiff。
所以,我想知道是否没有gdal函数可以直接改变图像并随意增加/减少亮度!
代码当前看起来像这样(也试图改变对比度,但是如果我了解如何改变亮度,我可以找到方法):
# 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为我提供的最新功能,但它不再是一种落后的解决方案,我正在研究它。