gdal 使用 python 转换比例并保存为 jpg
gdal translate scale and save to jpg using python
我知道如何使用 gdal translate 缩放并通过 cmd 行保存为 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]])
我认为的问题是如何正确selectscaleParams
。似乎 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.
关于如何 select 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)
根据这些统计数据,您应该能够得出一些所需的拉伸。如果您想在每个波段的最小值和最大值之间进行缩放,您可以这样做:
scaleParams = list(zip(*[vmin, vmax]))
或者,如果您想使用绝对最高和最低(所有波段)
scaleParams = [[min(vmin), max(vmax)]]
等等
我知道如何使用 gdal translate 缩放并通过 cmd 行保存为 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]])
我认为的问题是如何正确selectscaleParams
。似乎 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.
关于如何 select 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)
根据这些统计数据,您应该能够得出一些所需的拉伸。如果您想在每个波段的最小值和最大值之间进行缩放,您可以这样做:
scaleParams = list(zip(*[vmin, vmax]))
或者,如果您想使用绝对最高和最低(所有波段)
scaleParams = [[min(vmin), max(vmax)]]
等等