我知道如何使用 gdal 翻译来缩放并通过命令行保存为 jpg:
gdal_translate image.bsq image.jpg -of JPEG -outsize 10% 10% -scale
这会产生(我称之为漂亮的图像):
我想通过 python 生成类似的图像,例如:
from osgeo import gdal
img_bsq = 'image.bsq'
img_jpg = 'image.jpg'
gdal.Translate(img_jpg, img_bsq, format='JPEG', width=1024, height=0, scaleParams=[[500,1000,10,20]])
我认为的问题是如何正确选择scaleParams
。 cmd 行上的 -scale
似乎会自动计算值,按照 man gdal_translate
:
-scale [src_min src_max [dst_min dst_max]]:
Rescale the input pixels values from the range src_min to src_max to the range dst_min to dst_max. If omitted the output range is 0
to 255. If omitted the input range is automatically computed from the source data.
有关如何选择scaleParams
(或其他相关选项)的任何提示?
最佳答案
您也应该可以将其留空,例如:
gdal.Translate(img_jpg, img_bsq, format='JPEG', width=1024, height=0, scaleParams=[[]])
这会导致 GDAL 自行进行猜测,如文档中所述:
-scale [src_min src_max [dst_min dst_max]]: Rescale the input pixels values from the range src_min to src_max to the range dst_min to dst_max. If omitted the output range is 0 to 255. If omitted the input range is automatically computed from the source data.
http://www.gdal.org/gdal_translate.html
或者,您也可以检索统计数据(每个频段)并自己编写一些内容。
获取统计数据:
ds = gdal.Open('img_bsq')
stats = [ds.GetRasterBand(i+1).GetStatistics(True, True) for i in range(ds.RasterCount)]
ds = None
vmin, vmax, vmean, vstd = zip(*stats)
根据这些统计数据,您应该能够提出一些所需的拉伸(stretch)。如果您想在每个频段的最小值和最大值之间进行缩放,您可以这样做:
scaleParams = list(zip(*[vmin, vmax]))
或者,如果您想使用绝对最高和最低(所有频段)
scaleParams = [[min(vmin), max(vmax)]]
等等
关于python - gdal使用python翻译比例并保存为jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50504205/