如下:
path = '输入文件所在的文件夹路径'import os
import numpy as np
import tifffile as tiffdef read_raw_data(path, dimensions, dtype):"""读取 .raw 文件并返回图像数据:param path: .raw 文件路径:param dimensions: 图像的行数和列数 (height, width):param dtype: 数据类型 (如 'uint16'):return: 图像数据的 numpy 数组"""with open(path, 'rb') as f:img = np.fromfile(f, dtype=dtype)img = img.reshape(dimensions)return img# 在这里修改存放 raw 文件的文件夹路径
pro_path = '/Users/bcy/Desktop/论文研究2/研究论文/raw转换成tif代码/'
info_dimension = (1536, 1536) # 数据维度 (height, width)
dtype = 'uint16'# 输出文件夹路径
output_folder = '输出数据所在文件夹的路径'
if not os.path.exists(output_folder):os.makedirs(output_folder)print('--批量转换开始--')for i in range(60, 601, 60):# 构建 .raw 文件路径raw_filename = f'I1-{i}.raw'raw_path = os.path.join(pro_path, raw_filename)# 检查文件是否存在if os.path.exists(raw_path):# 读取 .raw 文件img_raw = read_raw_data(raw_path, info_dimension, dtype)# 转置图像img_raw = img_raw.T# 构建 .tif 文件路径tif_filename = f'{i}.tif'tif_path = os.path.join(output_folder, tif_filename)# 保存为 .tif 文件tiff.imwrite(tif_path, img_raw, photometric='minisblack', dtype=img_raw.dtype)print(f'{raw_filename} 转换完成')else:print(f'{raw_path} 文件不存在')print('--批量转换结束--')