python tif转jpg
2021/9/5 17:08:25
本文主要是介绍python tif转jpg,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在同级目录完成tif和jpg的批量转换
import os import cv2 import numpy as np from osgeo import gdal #数据格式转化 def normalization(data): _range = np.max(data) - np.min(data) return (data - np.min(data)) / _range def imgto8bit(img): img_nrm = normalization(img) img_8 = np.uint8(255 * img_nrm) return img_8 def tif_jpg(rasterfile): in_ds = gdal.Open(rasterfile) # 打开样本文件 xsize = in_ds.RasterXSize # 获取行列数 ysize = in_ds.RasterYSize bands = in_ds.RasterCount B_band = in_ds.GetRasterBand(1) B= B_band.ReadAsArray(0, 0, xsize, ysize).astype(np.int16) G_band = in_ds.GetRasterBand(2) G = G_band.ReadAsArray(0, 0, xsize, ysize).astype(np.int16) R_band = in_ds.GetRasterBand(3) R = R_band.ReadAsArray(0, 0, xsize, ysize).astype(np.int16) R1 = imgto8bit(R) G1 = imgto8bit(G) B1 = imgto8bit(B) data2= cv2.merge([R1,G1,B1]) return data2 if __name__ == '__main__': path=r"F:\algorithm\算法练习\拼接与镶嵌\test2_next" classs = os.listdir(path) for idx, folder in enumerate(classs): if folder.endswith('tif') or folder.endswith('tiff') : ori_image = os.path.join(path, folder) print(ori_image) if folder.endswith('tiff'): result_name = os.path.basename(ori_image)[:-5] else: result_name = os.path.basename(ori_image)[:-4] # print(result_name) a = os.path.dirname(ori_image) out = a + "\\" + result_name + ".jpg" img=tif_jpg(ori_image) cv2.imencode('.jpg', img)[1].tofile(out)
这篇关于python tif转jpg的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南