随笔 - 633,  文章 - 0,  评论 - 13,  阅读 - 48万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
代码:
复制代码
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)
复制代码

 

posted on   大话人生  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 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编写快捷键
点击右上角即可分享
微信分享提示