python - gdal使用python翻译比例并保存为jpg

标签 python image gis gdal

我知道如何使用 gdal 翻译来缩放并通过命令行保存为 jpg:

gdal_translate image.bsq image.jpg -of JPEG -outsize 10% 10% -scale

这会产生(我称之为漂亮的图像):

enter image description here

我想通过 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/

相关文章:

在循环中执行 SQL 的 Python 脚本

google-maps - 自定义Google Map叠加层?

python - 发送的scapy包收不到

python - 并行线程 python GIL 与 Java

python - 有效检查 numpy ndarray 值是否严格增加

Android SurfaceView 在视频后显示图像(清晰表面)

html - CSS 和 DIV 对齐

image - IE8 中 "background-size"CSS 属性的解决方法,尝试分层两个 div

java - 找到最近点(几何点)

r - 如何从地理栅格获取 `ppm` 的协变量数据?