![](/img/trans.png)
[英]How to save images with the same name to the different direction using PIL and os.path?
[英]How can I save images with different name on different directory using PIL .save?
我試圖從目錄'./train_dataset/train_img_cropped'和'./train_dataset/train_gt_cropped'中獲取train_img和ground truth img。 接下來,我想保存原始圖像並在目錄'./train_dataset/train_img_preprocessed'和'./train_dataset/train_gt_preprocessed'中保存原始圖像和帶有'_0'、'_1'尾部的翻轉圖像。 但是將名稱(文件+“_0”或“_1”)更改為未知文件擴展名時出現錯誤。 看起來 PIL 以某種方式將 _0、_1 識別為擴展名。 有沒有人可以幫我改名字省錢?
import os
import os.path
import glob
from PIL import Image
def preprocess(img_path, save_path):
targetdir = img_path
files = os.listdir(targetdir)
format = [".png"]
for (path, dirs, files) in os.walk(targetdir):
for file, i in files:
if file.endswith(tuple(format)):
image = Image.open(path + "/" + file)
image.save(save_path + "/" + file)
flippedImage = image.transpose(Image.FLIP_LEFT_RIGHT)
flippedImage.save(save_path + "/" + file)
print(file + " successfully flipped!")
else:
print(path)
print("InValid", file)
if __name__ == "__main__":
train_img_cropped_path = './train_dataset/train_img_cropped'
train_img_preprocessed_path = './train_dataset/train_img_preprocessed'
train_gt_cropped_path = './train_dataset/train_gt_cropped'
train_gt_preprocessed_path = './train_dataset/train_gt_preprocessed'
preprocess(train_img_cropped_path, train_img_preprocessed_path)
preprocess(train_gt_cropped_path, train_gt_preprocessed_path)
不確定這是否回答了您的問題,但為什么不使用臨時名稱(例如隨機字母數字字符串或 uuid)保存圖像,然后使用os.rename
更改臨時文件的名稱,並以您想要的名稱結尾_0
或_1
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.