代码:
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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2022-06-18 mybatis之sql执行有数据但返回结果为null
2022-06-18 SpringBoot之swagger
2022-06-18 springboot编写快捷键