光栅图像的叠加及python压缩

2024-09-27 07:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试堆叠光栅.tif文件我有12个.tif文件,但我还想用python压缩输出堆叠文件

这是我当前用于堆叠文件的工作代码,但堆叠后文件的大小非常大

from osgeo import gdal
outvrt='/vsimen/Stacked.tif'
outtif='E:/Users/Compressed_files/Stacked.tif'
tifs=glob.glob('E:/Users/Compressed_files/*.tif')
outds=gdal.BuildVRT(outvrt,tifs,seperate=True)
outds=gdal.Translate(outtif,outds)

Tags: 文件代码from光栅filescompressedusersglob
1条回答
网友
1楼 · 发布于 2024-09-27 07:30:25

可以将TranslateOptions对象传递给gdalTranslate调用,在该调用中可以添加相关的creationOptions进行压缩

因此,作为虚拟代码:

from osgeo import gdal

topts = gdal.TranslateOptions(creationOptions=['COMPRESS=LZW', 'PREDICTOR=2'])
outds=gdal.Translate(outtif,outds, options=topts)

当然,这些选项也可以是一个字符串或字符串数组-不必使用TranslateOptions

此外,虚拟代码中使用的压缩设置只是一个示例。为了最大化您的收益,您应该选择与您的数据相关的选项

以下是documentation on available options for GeoTiffs的摘录:

COMPRESS=JPEG/LZW/PACKBITS/DEFLATE/CCITTRLE/CCITTFAX3/CCITTFAX4/LZMA/ZSTD/LERC/LERC_DEFLATE/LERC_ZSTD/WEBP/NONE]: Set the compression to use. JPEG should generally only be used with Byte data (8 bit per channel). But starting with GDAL 1.7.0 and provided that GDAL is built with internal libtiff and libjpeg, it is possible to read and write TIFF files with 12bit JPEG compressed TIFF files (seen as UInt16 bands with NBITS=12). See the “8 and 12 bit JPEG in TIFF” wiki page for more details. The CCITT compression should only be used with 1bit (NBITS=1) data. LZW, DEFLATE and ZSTD compressions can be used with the PREDICTOR creation option. ZSTD is available since GDAL 2.3 when using internal libtiff and if GDAL built against libzstd >=1.0, or if built against external libtiff with zstd support. LERC/LERC_DEFLATE/LERC_ZSTD are available since GDAL 2.4 when using internal libtiff (and for LERC_ZSTD, see above mentioned conditions). None is the default.

NUM_THREADS=number_of_threads/ALL_CPUS: (From GDAL 2.1) Enable multi-threaded compression by specifying the number of worker threads. Worth for slow compressions such as DEFLATE or LZMA. Will be ignored for JPEG. Default is compression in the main thread.

PREDICTOR=[1/2/3]: Set the predictor for LZW, DEFLATE and ZSTD compression. The default is 1 (no predictor), 2 is horizontal differencing and 3 is floating point prediction.

相关问题 更多 >

    热门问题