转换tif到jpg

发布时间 2023-04-27 16:14:28作者: yokon


 1 import os,sys
 2 import cv2
 3 import numpy as np
 4 from skimage import io#使用IO库读取tif图片
 5 
 6 
 7 def tif_jpg_transform(file_path_name, bgr_savepath_name):
 8     img = io.imread(file_path_name)#读取文件名
 9     img = img / img.max()#使其所有值不大于一
10     img = img * 255 - 0.001  # 减去0.001防止变成负整型
11     img = img.astype(np.uint8)#强制转换成8位整型
12     # img = np.array([img,img,img])
13     # img = img.transpose(1,2,0)
14     # print(img.shape)  # 显示图片大小和深度
15     b = img[:, :, 0]  # 读取蓝通道
16     g = img[:, :, 1]  # 读取绿通道
17     r = img[:, :, 2]  # 读取红通道
18     bgr = cv2.merge([r, g, b])  # 通道拼接
19     cv2.imwrite(bgr_savepath_name, bgr)#图片存储
20 
21 
22 tif_file_path = r'D:\Pythster-VGGUP\testimg\B'# 为tif图片的文件夹路径
23 tif_fileList = os.listdir(tif_file_path)
24 for tif_file in tif_fileList:
25     file_path_name = tif_file_path + '/' + tif_file
26     jpg_path = r'D:\PythonPrVGGUP\testimg\bjpg' + '/' + tif_file.split('.')[0] + '.jpg' #.jpg图片的保存路径
27     tif_jpg_transform(file_path_name, jpg_path)
28     print(tif_file.split('.')[0])