python批量修改图片名称并更换格式

path_name = '/home/server109/ADAXI/code/PyCharm_Projects/train' #原图片所在路径
i = 0
for item in os.listdir(path=path_name):
    new_item = str(i)+'.bmp'  #元图片均为.bmp格式
    os.rename(os.path.join(path_name,item), os.path.join(path_name,new_item))
    file_path = os.path.join(path_name, new_item)
    new_out = os.path.join(path_name, new_item)    # print(new_out)
    out_path = os.path.splitext((new_out))[0] + '.png'
    print(out_path)
    Image.open(file_path).save(out_path)
    os.remove(os.path.join(path_name, new_item))
    i+=1

该脚步将/train文件中的.bmp格式图片批量修改成.png图片并从0开始命名,效果如下:
python批量修改图片名称并更换格式_第1张图片
python批量修改图片名称并更换格式_第2张图片

你可能感兴趣的:(图像)