使用Python批量压缩tif文件操作步骤
1.前言
我在进行DEM数据的裁剪时,发现各个省的数据量非常大,比如说四川省的30m的DEM数据的大小为2G。考虑到有限的电脑磁盘空间,我对Tif文件采用了LZW压缩。
2.流程
文章图片
3.批量压缩代码
#文件夹中每个文件都进行压缩# -*- coding: utf-8 -*-import rasterio as rioimport rasterioimport osfrom 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 = https://www.it610.com/article/rio.open(rasterfile)#读取栅格rasterdata2= rasterdata.read()#获取栅格信息profile = rasterdata.profileprint(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.结果展示
![使用Python批量压缩tif文件操作步骤](https://img.it610.com/image/info11/8c8080479cc54238a2bd476abc6ac7ff.jpg)
文章图片
首先是四川省的原始文件大小为2.23Gb,压缩后的大小为0.99Gb,压缩了大概一半。
【使用Python批量压缩tif文件操作步骤】
![使用Python批量压缩tif文件操作步骤](https://img.it610.com/image/info11/e8536716f9cb427cbf00a7221b9a06d0.jpg)
文章图片
以上就是使用Python批量压缩tif文件操作步骤的详细内容,更多关于Python批量压缩文件的资料请关注脚本之家其它相关文章!
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- iOS中的Block