批量将PPM格式图片转化为JPG格式
生活随笔
收集整理的這篇文章主要介紹了
批量将PPM格式图片转化为JPG格式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
將PPM格式圖片轉化為JPG格式
做圖像識別的時候數(shù)據(jù)集常常是ppm格式的,雖然不影響建模訓練,但是我們電腦往往不支持ppm格式的圖像展示。
比如到做交通標志識別的時候用到的BelgiumTS交通數(shù)據(jù)集或者德國GTSRB數(shù)據(jù)集
下載后得到都是ppm格式的圖像。
格式轉化代碼
from PIL import Image import osinput_train_path = r"E:\test\BelgiumTSC_Training\Training" output_train_path = r"E:\BelgiumTSC_JPG\Train_data" input_test_path = r"E:\test\BelgiumTSC_Testing\Testing" output_test_path = r"E:\BelgiumTSC_JPG\Test_data"def batch_image(in_dir, out_dir):if not os.path.exists(out_dir):print(out_dir, 'is not existed.')os.mkdir(out_dir)if not os.path.exists(in_dir):print(in_dir, 'is not existed.')return -1directories = [d for d in os.listdir(in_dir) if os.path.isdir(os.path.join(in_dir, d))]for d in directories:# 每一類的路徑label_directory = os.path.join(in_dir, d)new_directory = os.path.join(out_dir, d)out_folder = os.path.exists(out_dir + d)if not out_folder:os.mkdir(new_directory)file_names = [os.path.join(label_directory, f) for f in os.listdir(label_directory) if f.endswith(".ppm")]# file_names is every photo which is end with ".ppm"count = 0for files in file_names:file_path, extfilename = os.path.split(files)filename, extname = os.path.splitext(extfilename)out_file = filename + '.jpg'# print(filepath,',',filename, ',', out_file)im = Image.open(files)new_path = os.path.join(new_directory, out_file)print(count, ',', new_path)count = count + 1im.save(new_path)if __name__ == '__main__':batch_image(input_test_path, output_test_path)batch_image(input_train_path, output_train_path)轉化后會保留原有的兩級目錄,方便建模訓練。
總結
以上是生活随笔為你收集整理的批量将PPM格式图片转化为JPG格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英特尔“Meteor Lake”酷睿 U
- 下一篇: 微软发布 Windows AI Stud