堆叠光栅图像并将其压缩为 python
Stacking of Raster Image and Compressing it in 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)
您可以将 TranslateOptions 对象传递给您的 gdalTranslate
调用,您可以在其中添加相关的 creationOptions
进行压缩。
因此作为虚拟代码:
from osgeo import gdal
topts = gdal.TranslateOptions(creationOptions=['COMPRESS=LZW', 'PREDICTOR=2'])
outds=gdal.Translate(outtif,outds, options=topts)
当然,选项也可以是字符串或字符串数组——不强制使用 TranslateOptions
。
此外,伪代码中使用的压缩设置只是一个示例。为了最大化您的收益,您应该 select 适合您的数据的选项。
以下是 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.
我正在尝试堆叠我的光栅 .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)
您可以将 TranslateOptions 对象传递给您的 gdalTranslate
调用,您可以在其中添加相关的 creationOptions
进行压缩。
因此作为虚拟代码:
from osgeo import gdal
topts = gdal.TranslateOptions(creationOptions=['COMPRESS=LZW', 'PREDICTOR=2'])
outds=gdal.Translate(outtif,outds, options=topts)
当然,选项也可以是字符串或字符串数组——不强制使用 TranslateOptions
。
此外,伪代码中使用的压缩设置只是一个示例。为了最大化您的收益,您应该 select 适合您的数据的选项。
以下是 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.