在同级目录完成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)