使用python批量转换.jfif文件为.jpg

python代码如下,有需要的自行取用:

需要引入Image库,方法是: pip install Image

 

import os
from PIL import Image

root_dir = r'C:\temp'

def list_files(root_dir):
    if os.path.isfile(root_dir):  # file
        print("请将root_dir设置为一个目录名而不是文件名")
        #if (os.path.splitext(root_dir)[-1] == '.jfif'):
            # jfif_file = root_dir
            # dest_path = os.path.join(dest_dir, jfif_file.strip('.jfif'))
            # img = Image.open(jfif_file)
            # img.save(dest_path + '.jpg')
            # print(root_dir + '=>' + dest_path + '.jpg')
    else:  # dir
        for file in os.listdir(root_dir):
            file_path = os.path.join(root_dir, file)
            if os.path.isfile(file_path):  # file
                if (os.path.splitext(file)[-1] == '.jfif'):
                    dest_path = os.path.join(root_dir, file.strip('.jfif'))
                    img = Image.open(file_path)
                    img.save(dest_path + '.jpg')
                    print(file_path)
            else:  # dir
                list_files(file_path)


if __name__ == '__main__':
    list_files(root_dir)

  

 

posted @ 2023-02-19 15:29  大厨无盐煮  阅读(390)  评论(0编辑  收藏  举报