代码:
import os from PIL import Image def convert_exit_to_png(directory): # 遍历指定目录 for filename in os.listdir(directory): # 检查文件扩展名是否为'.exit' if filename.endswith('.exif'): # 构建完整的文件路径 file_path = os.path.join(directory, filename) # 构建输出文件的路径,将扩展名改为'.png' output_path = os.path.join(directory, filename[:-5] + '.png') # 打开并转换图片 try: with Image.open(file_path) as img: img = img.convert('RGBA') # 转换为PNG格式,RGBA模式 img.save(output_path, 'PNG') print(f"Converted {filename} to PNG successfully.") except Exception as e: print(f"Failed to convert {filename}. Error: {e}") # 指定目录路径 directory_path = 'F:\\blender\\贴图\\笛子' convert_exit_to_png(directory_path)
将指定目录及其下所有文件夹下的内容转化:
import os from PIL import Image def convert_to_png(source_dir): # 遍历指定目录及其子目录 for root, dirs, files in os.walk(source_dir): for file in files: # 检查文件是否是图像文件,这里假设EXIF数据存储在图像文件中 if file.lower().endswith(('.exif')): # 构建完整的文件路径 file_path = os.path.join(root, file) # 尝试打开图像文件 try: with Image.open(file_path) as img: # 构建输出文件的路径,直接覆盖原文件 output_file_path = os.path.join(root, os.path.splitext(file)[0] + '.png') # 转换并保存为PNG格式 img.save(output_file_path, 'PNG') print(f'Converted {file_path} to {output_file_path}') except IOError: print(f'Failed to convert {file_path}') # 指定源目录 source_directory = r'F:\blender\贴图' # 调用函数执行转换 convert_to_png(source_directory)