简体   繁体   English

使用python将16位GeoTiff文件转换为8位JPEG文件

[英]Converting a 16bit GeoTiff file as an 8bit JPEG file using python

I am trying to convert a 16 bit 3-band RGB GeoTIFF file into an 8 bit 3-band JPEG file. 我正在尝试将16位3波段RGB GeoTIFF文件转换为8位3波段JPEG文件。 It seems like the gdal library should work well for this. 似乎gdal库应该可以很好地工作。 My question is how do I specify the conversion to 8-bit output in the python gdal API, and how do I scale the values in that conversion? 我的问题是如何在python gdal API中将转换指定为8位输出,以及如何缩放该转换中的值? Also, how do I check to tell whether the output is 8-bit or 16-bit? 另外,如何检查输出是8位还是16位?

The gdal.Translate() function should serve the purpose. gdal.Translate()函数应达到目的。 However, the only example that I found which will rescale the values to 8 bit involves the C interface. 但是,我发现的唯一将值重新缩放为8位的示例涉及C接口。 The two posts below provide examples of this, but again they do not suit my purpose because they are not using the Python interface. 下面的两篇文章提供了这样的示例,但它们又一次不适合我的目的,因为它们没有使用Python接口。

https://gis.stackexchange.com/questions/26249/how-to-convert-qgis-generated-tiff-images-into-jpg-jpeg-using-gdal-command-line/26252 https://gis.stackexchange.com/questions/26249/how-to-convert-qgis-generation-tiff-images-into-jpg-jpeg-using-gdal-command-line/26252

https://gis.stackexchange.com/questions/206537/geotiff-to-16-bit-tiff-png-or-bmp-image-for-heightmap/206786 https://gis.stackexchange.com/questions/206537/geotiff-to-16-bit-tiff-png-or-bmp-image-for-heightmap/206786

The python code that I came up with is: 我想出的python代码是:

from osgeo import gdal
gdal.Translate(destName='test.jpg', srcDS='test.tif')

This will work, but I don't think the output is coverted to 8-bit or that the values are rescaled. 这将起作用,但是我不认为输出被掩盖为8位或值被重新缩放。 Does anyone know how to apply those specific settings? 有谁知道如何应用这些特定设置?

Note that this post below is very similar, but uses the PIL package. 请注意,下面的帖子非常相似,但是使用了PIL包。 However the problem is that apparently PIL has trouble ingesting 16 bit images. 但是,问题在于,显然PIL无法摄取16位图像。 When I tried this code I got errors about reading the data. 当我尝试这段代码时,我在读取数据时遇到了错误。 Hence, I could not use this solution. 因此,我无法使用此解决方案。

converting tiff to jpeg in python 在Python中将tiff转换为jpeg

You could use options like this 您可以使用这样的选项

from osgeo import gdal

scale = '-scale min_val max_val'
options_list = [
    '-ot Byte',
    '-of JPEG',
    scale
] 
options_string = " ".join(options_list)

gdal.Translate('test.jpg',
               'test.tif',
               options=options_string)

Choose the min and max values you find suitable for your image as min_val and max_val 选择您认为适合图像的最小值和最大值作为min_valmax_val

If you like to expand scaling to the entire range, you can simply skip min and max value and just use scale = '-scale' 如果您想将缩放比例扩展到整个范围,则可以跳过最小值和最大值,而只需使用scale = '-scale'

I think the gdal way is to use gdal.TranslateOptions() . 我认为gdal的方式是使用gdal.TranslateOptions()

from osgeo import gdal

translate_options = gdal.TranslateOptions(format='JPEG',
                                          outputType=gdal.GDT_Byte,
                                          scaleParams=[''],
                                          # scaleParams=[min_val, max_val],
                                          )

gdal.Translate(destName='test.jpg', srcDS='test.tif', options=translate_options)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM