华域联盟 Python 使用Python批量压缩tif文件操作步骤

使用Python批量压缩tif文件操作步骤

1.前言

我在进行DEM数据的裁剪时,发现各个省的数据量非常大,比如说四川省的30m的DEM数据的大小为2G。考虑到有限的电脑磁盘空间,我对Tif文件采用了LZW压缩。

2.流程

3.批量压缩代码

#文件夹中每个文件都进行压缩
# -*- coding: utf-8 -*-
import rasterio as rio
import rasterio
import os
from tqdm import tqdm
#每个线程选择一个文件夹
Input_path ="输入文件夹"+"\\"
Output_path ="输出文件夹"+"\\"
#文件列表
pathDir= os.listdir(Input_path)
#压缩函数
for i in tqdm(range(len(pathDir))):
    # 读入栅格文件
    rasterfile = Input_path+"\\"+pathDir[i]
    #打开栅格
    rasterdata = rio.open(rasterfile)
    #读取栅格
    rasterdata2= rasterdata.read()
    #获取栅格信息
    profile = rasterdata.profile
    print(profile)
    #选择压缩方式
    profile.update(
        compress='lzw',  #压缩方式:rle,lzw等
        )
    #导出文件路径与名字
    out_put_name=Output_path +"RLE"+pathDir[i]
    #导出
    with rasterio.open(out_put_name, mode='w', **profile) as dst:
        dst.write(rasterdata2)

4.结果展示

首先是四川省的原始文件大小为2.23Gb,压缩后的大小为0.99Gb,压缩了大概一半。

以上就是使用Python批量压缩tif文件操作步骤的详细内容,更多关于Python批量压缩文件的资料请关注华域联盟其它相关文章!

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » 使用Python批量压缩tif文件操作步骤

转载请保留出处和原文链接:https://www.cnhackhy.com/35144.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部